Populating a dropdown list based on another dropdown

Possible duplicate:
Fill in one drop-down menu based on a selection in another

I have 3 dropdowns on my aspx page. The first one is hardcoded with 4 values. The second should be populated programmatically, for example. if I select "Product1" in the first drop-down list, then the second drop-down menu should be filled with values ​​such as "Model1_1", "Model1_2". If I select "Product2", then the second drop-down menu will be populated with "Model2_1", "Model2_2").

could you help me?

+4
source share
3 answers

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

2 In your deletet, when you send data, bind the second DropDownList2 with the prefix SelectedValue

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

I could name 2 solutions to these issues.

First, using the Cascading Drop Down from the AJAX Toolkit for ASP.NET pages. You have links and an example: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx This is somehow a cleaner , and it does not cause a postback, but you should use this toolkit. But it’s nice to learn how to use it, because it offers others pleasant amenities.

The second involves adding an OnSelectedIndexChange event handler for the DropDownList. Therefore, when the user selects a value from the first drop-down list, the server side catches this event and fills the second DropDown with the necessary values. Due to the fact that this requires server-side operations, it can be quite frustrating to reload the page after selection. The client side should look like this:

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

Where on the server side:

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

you can populate the second drp (DropDownList) in the following ways: Connect the 1st drp to the table that calls this query: (in SQLDataSource1)

 SELECT [id], [name] FROM Products 

in the second drp you should call (in SQLDataSource2)

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

in SQLDataSource2 select command The property to add a parameter value for Product_id should be drp1.SelectedValue

Note. Remember to Enable AutoPostBack for drp1

+2
source

Source: https://habr.com/ru/post/1432211/


All Articles