【问题标题】:How to use the DropDownList's SelectedIndexChanged event如何使用 DropDownList 的 SelectedIndexChanged 事件
【发布时间】:2013-10-17 22:25:18
【问题描述】:

我的网络表单中有两个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(); 
}

【问题讨论】:

  • 您当前拥有的代码会发生什么?

标签: c# asp.net webforms event-handling


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 很清楚 Ketan,两个 drobdownlists,更改第一个会触发 SelectedIndexChanged 事件。在事件处理程序“DropDownList1_SelectedIndexChanged”中,您需要根据第一个下拉列表的值加载第二个下拉列表...
    【解决方案3】:

    我认为这是罪魁祸首:

    cmd = new SqlCommand(query, con);
    
    DataTable dt = Select(query);
    
    cmd.ExecuteNonQuery();
    
    ddtype.DataSource = dt;
    

    我不知道该代码应该做什么,但看起来您想为此创建一个 SqlDataReader,正如 here 所解释的那样,如果您搜索“SqlCommand DropDownList DataSource”,则在整个网络上:

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

    或者您可以按照here 的说明创建一个DataTable

    cmd = new SqlCommand(query, con);
    
    SqlDataAdapter listQueryAdapter = new SqlDataAdapter(cmd);
    DataTable listTable = new DataTable();
    listQueryAdapter.Fill(listTable);
    
    ddtype.DataSource = listTable;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      相关资源
      最近更新 更多