C# 如何使用 DropDownList 的 SelectedIndexChanged 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19438389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:59:11  来源:igfitidea点击:

How to use the DropDownList's SelectedIndexChanged event

c#asp.netwebformsevent-handling

提问by Ketan

I have two DropDownLists in my webform and when I select a value in the first dropdownlist, I would like a related value to be automatically selected in the second dropdownlist.

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

This is what I currently have:

这是我目前拥有的:

    <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>

Code Behind :

背后的代码:

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

回答by Afshin

The most basic way you can do this in SelectedIndexChanged events of DropDownLists. Check this code..

您可以在 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
}

回答by CodeCaster

I think this is the culprit:

我认为这是罪魁祸首:

cmd = new SqlCommand(query, con);

DataTable dt = Select(query);

cmd.ExecuteNonQuery();

ddtype.DataSource = dt;

I don't know what that code is supposed to do, but it looks like you want to create an SqlDataReaderfor that, as explained hereand all over the web if you search for "SqlCommand DropDownList DataSource":

我不知道该代码应该做什么,但是如果您搜索“SqlCommand DropDownList DataSource” ,则看起来您想为此创建一个SqlDataReader,如here和整个网络所述:

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

Or you can create a DataTableas explained here:

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

cmd = new SqlCommand(query, con);

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

ddtype.DataSource = listTable;

回答by Mr Phuc 87

You should add AutoPostBack="true" to DropDownList1

您应该将 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>