私は 2 つの DropDownLists を持っています。最初のドロップダウン リストには、1 つのフィールドのみを持つ SqlDataSource があり、別の DropDownList には、最初のドロップダウンの選択に基づく where 句を持つ SqlDataSource があります。
ボタンを押すと、2 番目のドロップダウン リストにクエリが入力されますが、最初の項目から項目を選択した後に入力するにはどうすればよいですか。
2 番目のドロップダウン リストのクエリのメソッドを selectionChangeCommited イベントに追加することで、自分のプロジェクトの 1 つでこれを機能させました。このようなもの:
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
fillComboBox2();
}
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
//Your SqlDataSource must have a parameter for selecting its records ("YourParameter")
//P.E. the SqlDataSource SelectCommand looks like this:
//"SELECT [id], [name] FROM [table] WHERE [id] = @YourParameter"
//Include a selectparameter <asp:Parameter Name="YourParameter" Type="Int32" />
//inside your SqlDataSource. Next, do this:
sqldatasource2.SelectParameters("YourParameter").DefaultValue = comboBox1.SelectedValue
comboBox2.DataSource = sqldatasource2.Select(DataSourceSelectArguments.Empty)
comboBox2.DataBind()
}