我的网络表单中有两个DropDownList
,当我在第一个下拉列表中选择一个值时,我希望在第二个下拉列表中自动选择一个相关值。
这就是我目前所拥有的:
<table>
<tr>
<td>
<asp:Label ID="lbmanu" runat="server" Text="Furniture Manufacturer :
"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddmanu" runat="server"
DataSourceID="Sql_fur_model_manu"
DataTextField="manufacturer" DataValueField="manufacturer"
onselectedindexchanged="ddmanu_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="Sql_fur_model_manu" runat="server"
ConnectionString="<%$ ConnectionStrings:conStr %>"
SelectCommand="SELECT DISTINCT [manufacturer] FROM
[furniture_manufacturer]">
</asp:SqlDataSource>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbtype" runat="server" Text="Furniture Type :
"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddtype" runat="server" AutoPostBack="True">
</asp:DropDownList>
</td>
</tr>
</table>
代码背后:
protected void ddmanu_SelectedIndexChanged(object sender, EventArgs e)
{
string query = "select furniture from furniture_model where manufacturer='" +
ddmanu.SelectedValue.ToString() + "'";
con.Open();
cmd = new SqlCommand(query, con);
DataTable dt = Select(query);
cmd.ExecuteNonQuery();
ddtype.DataSource = dt;
ddtype.DataTextField = "manufacturer";
ddtype.DataValueField = "furniture";
ddtype.DataBind();
}
答案 0 :(得分:5)
你应该添加AutoPostBack =&#34; true&#34;到DropDownList1
<asp:DropDownList ID="ddmanu" runat="server" AutoPostBack="true"
DataSourceID="Sql_fur_model_manu"
DataTextField="manufacturer" DataValueField="manufacturer"
onselectedindexchanged="ddmanu_SelectedIndexChanged">
</asp:DropDownList>
答案 1 :(得分:4)
您可以在DropDownLists的SelectedIndexChanged事件中执行此操作的最基本方法。检查此代码..
<asp:DropDownList ID="DropDownList1" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="224px"
AutoPostBack="True" AppendDataBoundItems="true">
<asp:DropDownList ID="DropDownList2" runat="server"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//Load DropDownList2
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
//Load DropDownList3
}
答案 2 :(得分:0)
我认为这是罪魁祸首:
cmd = new SqlCommand(query, con);
DataTable dt = Select(query);
cmd.ExecuteNonQuery();
ddtype.DataSource = dt;
我不知道该代码应该做什么,但看起来你想为SqlDataReader
创建一个cmd = new SqlCommand(query, con);
ddtype.DataSource = cmd.ExecuteReader();
,正如here解释的那样,如果你搜索“ SqlCommand DropDownList DataSource“:
DataTable
或者您可以按照here说明创建cmd = new SqlCommand(query, con);
SqlDataAdapter listQueryAdapter = new SqlDataAdapter(cmd);
DataTable listTable = new DataTable();
listQueryAdapter.Fill(listTable);
ddtype.DataSource = listTable;
:
{{1}}
答案 3 :(得分:0)
CS1061:ASP.webform1_aspx
不包含SAMPLE_OnSelectedIndexChanged
的定义,并且没有可以找到接受SAMPLE_OnSelectedIndexChanged
类型的第一个参数的扩展方法ASP.webform1_aspx
(您是否错过了使用指令或程序集引用?)
答案 4 :(得分:0)
使用下拉列表AutoPostBack
的{{1}}将true
属性设置为AutoPostBack="true"
。