C# 根据另一个下拉列表填充下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12258839/
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-09 22:39:25  来源:igfitidea点击:

Populate Dropdown list based on another dropdown list

c#

提问by N K

Possible Duplicate:
Populate one dropdown based on selection in another

可能的重复:
根据另一个下拉列表中的选择填充一个下拉列表

I have 3 Dropdown list on my aspx page. First one is hard coded with 4 values. The second one should be populated programatically, for ex. if I select "Product1" in first dropdown, then second dropdown should be populated with values like "Model1_1", "Model1_2". If I select "Product2" then the second dropdown is populated with "Model2_1", "Model2_2").

我的 aspx 页面上有 3 个下拉列表。第一个是用 4 个值硬编码的。第二个应该以编程方式填充,例如。如果我在第一个下拉列表中选择“Product1”,那么第二个下拉列表应该填充“Model1_1”、“Model1_2”等值。如果我选择“Product2”,那么第二个下拉列表将填充“Model2_1”、“Model2_2”)。

Could you give me some help ?

你能给我一些帮助吗?

采纳答案by Aghilas Yakoub

1 Set AutoPostBack="true"on first DropDownList1 and add OnSelectIndexChanged="YourDelegate".

1AutoPostBack="true"在第一个 DropDownList1 上设置并添加OnSelectIndexChanged="YourDelegate".

2 In your delegate when you post data, bind your second DropDownList2 with prefix SelectedValue

2 在您发布数据时的委托中,将您的第二个 DropDownList2 与前缀 SelectedValue 绑定

 protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
 {
   var value =  DropDownList1.SelectedValue;
   if(value == "Product1")
   { 
    ....      
   }
   else if(value == "Product2")
   { 
    ....      
   }
 }

回答by Coral Doe

I could name 2 solutions to this questions.

对于这个问题,我可以说出 2 个解决方案。

First one involves using Cascading Drop Down from the AJAX Toolkit for ASP.NET pages. You have here references and an example: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspxIt is somehow cleanerand it doesn't cause postbacks, but you have to use that toolkit. But it is nice to learn to use it, because it offers others nice facilities.

第一个涉及使用 AJAX Toolkit for ASP.NET 页面中的 Cascading Drop Down。你在这里有参考资料和一个例子: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx不知何故cleaner,它不会导致回发,但你必须使用该工具包。但是学习使用它很好,因为它为其他人提供了很好的设施。

The second involves adding for your DropDownList a handler for the OnSelectedIndexChange event. So when the user selects a value from the first dropdown, the server side catches the event and populates the second DropDown with the necessary values. Due to the fact that this requires server side operations, it can be rather annoying to reload the page after a selection is made. The client side should look like this:

第二个涉及为您的 DropDownList 添加 OnSelectedIndexChange 事件的处理程序。因此,当用户从第一个下拉列表中选择一个值时,服务器端会捕获该事件并使用必要的值填充第二个下拉列表。由于这需要服务器端操作,因此在做出选择后重新加载页面可能会很烦人。客户端应如下所示:

  <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack = true OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>

Where in the server side:

在服务器端的位置:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList2.Items.Clear();
    DropDownList2.Items.Add("text");
    ....
}

回答by Emin Hasanov

you can populate second drp (DropDownList) by this ways: Connect 1st drp with table, which calls this query: (in SQLDataSource1)

您可以通过以下方式填充第二个drp(DropDownList):将第一个drp与表连接,该表调用此查询:(在SQLDataSource1中)

SELECT [id], [name] FROM Products

in 2nd drp you should call (in SQLDataSource2)

在第二个 drp 中,您应该调用(在 SQLDataSource2 中)

 SELECT [id], [name] FROM Models WHERE ProductID=@Product_id

in SQLDataSource2select commandProperty add Parameter value for Product_idit must be drp1.SelectedValue

SQLDataSource2select command属性中添加参数值Product_id必须是drp1.SelectedValue

Note: Don't forget Enable AutoPostBackfor drp1

注意:不要忘记Enable AutoPostBackdrp1