2

我的网络表单中有两个DropDownLists,当我在第一个下拉列表中选择一个值时,我希望在第二个下拉列表中自动选择一个相关值。

这是我目前拥有的:

    <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(); 
}
4

3 回答 3

8

您应该将 AutoPostBack="true" 添加到 DropDownList1

                <asp:DropDownList ID="ddmanu" runat="server" AutoPostBack="true"
                    DataSourceID="Sql_fur_model_manu"    
                    DataTextField="manufacturer" DataValueField="manufacturer" 
                    onselectedindexchanged="ddmanu_SelectedIndexChanged">
                </asp:DropDownList>
于 2014-04-24T07:19:59.443 回答
5

您可以在 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
}
于 2013-10-17T22:38:42.627 回答
0

我认为这是罪魁祸首:

cmd = new SqlCommand(query, con);

DataTable dt = Select(query);

cmd.ExecuteNonQuery();

ddtype.DataSource = dt;

我不知道该代码应该做什么,但看起来你想为此创建一个,如果你搜索“SqlCommand DropDownList DataSource” ,这里SqlDataReader和整个网络都有解释:

cmd = new SqlCommand(query, con);
ddtype.DataSource = cmd.ExecuteReader();

或者您可以按照此处DataTable的说明创建一个:

cmd = new SqlCommand(query, con);

SqlDataAdapter listQueryAdapter = new SqlDataAdapter(cmd);
DataTable listTable = new DataTable();
listQueryAdapter.Fill(listTable);

ddtype.DataSource = listTable;
于 2013-10-17T22:51:14.280 回答