How to get the list of second drop-down-list based on value selected in the first drop-down-list

I have two drop-down-lists, the second one depends on the value chosen in the first drop-down-list. 

In the first drop-down-list, I have AutoPostBack = "true" and OnSelectedIndexChanged = "SelectedIndexChanged"

In the selectedIndexChanged method, I do the following:

1    protected void SelectedIndexChanged(object sender, EventArgs e)
2        {
3            if (IsPostBack)
4            {
5                string assemblyNumber = ddlAssemblyNumber.SelectedValue;
6                DataSet routeIdDS = RoutesBusiness.GetRouteId(assemblyNumber);
7                string routeId = routeIdDS.Tables[0].Rows[0]["RouteId"].ToString();
8                ddlRoute.DataSource = RoutesBusiness.GetRouteId(assemblyNumber);
10               ddlRoute.DataTextField = "RouteId";
11               ddlRoute.DataBind();
12           }
13       }

ddlAssemblyNumber is the first drop-down-list and ddlRoute is the second drop-down-list.  I put a breakpoint and check to make sure that the value I got back is right and it is right but somehow, it doesn't bind to the second drop-down-list.

Would anyone please tell me what I do wrong?  Thanks in advance!


6/26/2008 4:33:03 PM 93655 articles. 5 followers. Follow

7 Replies

Similar Articles

[PageSpeed] 2

First check if ddlAssemblyNumber.SelectedValue; returns a Value.. If it does then try this snippet below

1    protected void SelectedIndexChanged(object sender, EventArgs e)
2    {

5                string assemblyNumber = ddlAssemblyNumber.SelectedValue;
6                DataSet routeIdDS = RoutesBusiness.GetRouteId(assemblyNumber);
7                string routeId = routeIdDS.Tables[0].Rows[0]["RouteId"].ToString();
                     if (routeIdDS.Tables[0].Rows.Count > 0)
8                    ddlRoute.DataSource = routeIdDS;
10                   ddlRoute.DataTextField = "RouteId";
11                   ddlRoute.DataBind();

13    }



"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
6/26/2008 5:06:22 PM

No, it's still not working.  I'm not sure if it's doing something with AutoPostBack or not.

Does anybody have any idea?


6/26/2008 6:09:42 PM

How do you Bind your First DLL?If you are Biding your DDL1 on Page_Load event then be sure to wrap it inside Not IsPostBack method

if (!Page.IsPostBack)


    //Bind your DDL1 here 




"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
6/26/2008 6:14:53 PM

I have the UpdatePanel that have those two drop-down-list in it.  And when the page load, this UpdatePanel is set to invisible.  Then when someone click on a button, this UpdatePanel becomes visible (so I think it's the post back) and so, when the first drop-down-list being chosen again, it cannot do another post back, hence the second drop-down-list still keeps its first values that on the first post back it has.

So, now my question is "can you do multiple post back on a page and if yes, how to do it? if not, is there a work around for it?" or I should have the UpdatePanel as a stand alone page?



6/26/2008 6:39:35 PM

I think I figure it out.  In Page_Load, I just bind the data to the two drop-down-list but having the UpdatePanel.Visible = false, and when a button is clicked, set UpdatePanel.Visible = true and then the PostBack works.  The only problem is that the second drop-down-list still keeps the previous lists and adding the new one so how can I clear the second drop-down-list and then bind to the new list?


6/26/2008 7:10:55 PM

Got it!


Big Smile

6/26/2008 7:17:12 PM


how can I clear the second drop-down-list and then bind to the new list?


//Bind your DDL after clearing 


"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
6/26/2008 7:22:30 PM

Similar Artilces:

Populate a second drop down list upon selection value of the first drop down list in a gridview
How to Populate a second drop down list upon selection value of the first drop down list in a gridview? DropDownList1 ******************* <asp:TemplateField HeaderText="SystemName" SortExpression="SystemName">                <EditItemTemplate>                    <asp:DropDownList ID="sysidddl" runat="server" DataSourceID="sysods1"    &nbs...

Populate 2nd drop-down list box based on selected values in 1st drop-down list box
Hi Group,I have two DDL boxes on my page.  DDL one has a list of values and a code.e.g code  ValueABC   123ABC   345FGH   123 DDL 2 should only show values for the code selected in  DDL1 e.g  if user selects record 1 with code ABCthen DDL2 should show ALL records with the linked field value of ABC.The tables are normalized.  For your info, the name of the field of the value I would like to check is called sublistcode.Using  TIA         Try this code: Note: Set appenddataboun...

Drop Down List in a Data List =- How can I select a specific item in Drop Down List
I have put a drop down list into a data list. It will have a list of cities in it related to the market I am writing this for. I need to have this select the city where a current customer is doing business so that if they move they can use this drop down to select the city they moved to in the market How do I set a default row in the dropdown? I now goes to row 0, probably the wrong one If you know what the default value is supposed to be, the easiest way to preset the dropdownlist control is to write code in the ItemDataBound event of the DataList using the FindControl method to get ...

FormView: Filter a drop-down list based on a selection in another drop-down list
Hi Everyone, I have a FormView and in it is a drop-down list for "Clients". Below it is another drop-down list: "Contact". The database is set-up so that each clinet has one or more contacts. So, when I change the selected client, I want the list of contact to update. Here's the code: '> " SelectCommand="SELECT [Client_ID], [Client_Name_Short] FROM [tbl_Clients] WHERE ([Is_Active] = @Is_Active) ORDER BY [Client_Name_Short]"> ...

How to populate one drop down list based on the item selected in another drop down list?
Hello Mate,                      I have two drop down lists. I have to populate one drop down list from Database  based on the selection of an item in another drop down list. And i dont want the page to be posted back while performing this operation.                      If you have an idea about my issue, Please let me know!.Much Obliged & Thanks a Lot,Myself Hi, you can craft ...

Drop Down List Populated Depending Upon the value Selected in another Drop Down List
  I have five drop down lists.Each One populated based on the value selected in the previious one. I have used SqlDataSource for binding data to each drop down.And I need some initial value so I put "AppendDataBoundItems" as true.Functionality is ok. The problem is, I have selected one from first dropdown.then second drop down is populated.but now i want to select a different value from first dropdown.If i do so,To my second drop down list the values r being appended.not refreshed.I think this is because AppendDataBoundItems=true Plz suggest me something. Thanks in Advan...

Update 2 drop-down list based on values of the 1 drop-down list box
 Hi,I am using ASP.NET,C# and SQL ServerI have 2 Drop down list boxes.In first List box I have: countries ...I need to Update 2 text box with corresponding states...I am getting these values from database both country and states... How to implement this ..can anyone help me on this...Please provide some code examples ...It will be helpful... Did you checked Ajax CascadingDropDown ?  My Blog"Don't be afraid to be wrong; otherwise you'll never be right." Create a Sub to handle the SelectedIndexChanged of your Country drop down.  Anytime the country i...

Drop down list read its value from another drop down list
Hi, In Desktop that is so easy, either CBox.SelectedIndex = CBox_Master.SelectedIndex! but in ASP.Net i'm confused, here is the case Camps contain rooms, In rooms page, i have a drop down list contain the name of camps, its supposed that any room inserted or renderd in that page to belong to Camp in the drop down list, the selection is working good, now, i use a Form View to Insert new records in room table, in the insert template i place a drop down list for a user to chose from, by default its a textbox, and the camp_id is number, however i want shoe the camp name not number, so i dr...

Drop Down List defaults to first selection in list.
I have read a number of posts that pertain to this problem, but have not found a solution.I fill the first drop down from the database, pick a value and fill the second ddl from the database based on the first selection. I also insert a "Select Here" at position zero.  Then when I choose an item from the second ddl, it defaults back to the first item from the database, not the "Select Here", but the first database item.In various posts, the suggestion has been to have this code execute on the OnSelectedIndexChanged event.  I am doing this.  When it gets to the code for the sec...

List of lists (listing of)
Ted In the great List 'o lists., is there something which is along the lines of "Personal organisers" ? I have googled and found others - just wondering about the lists and couldn't see anything in there I have a intense, executive type who's after something along the lines of Lotus' personal organiser. Don't know why she wants to change from it when I know she already has it installed. Anybody else got suggestions for a good one? (and dont't ask what I mean by good - it's not for me - perhaps I mean effective) In grc.techtalk, Baz...

Cascading drop down lists: changing the primary list causes the secondary list to refresh BUT it retains the old selected value/item
I have two drop down lists. For the purposes of this example let's call them ddlState and ddlCity. ddlState is selected first, I may pick Texas.  Then the page refreshes and ddlCity has all the cities in Texas.  So I may pick the 5th one and it is Austin. Now I change my ddlState to California.  The ddlCity displays the first entry in the City list, HOWEVER, that entry is not selected.  The ddlCity.SelectedItem.ToString displays as Austin still. So the way I see it I have two options: 1. Insert a "Pick a City" programatiacally to the drop down list...

Drop down list state or getting the right value from the list
 Just a little background hopefully someone can help me ive been at this for about 1 week now i cant seem to get it. Im using 1.1 i dynamically load a bunch of drop down lists on a page from a sql 2k database. When i load the page everything is fine. The issue is when i postback. I select an index in a drop down but it doesnt stay  im trying to redirect when the value is chosen but all it is doing is reloading the dropdown with all the data Ive tried adding a button to grab the value of whats in the drop down but i cant do that if every time the page reloads back to the server it r...

How to get value from first dropdown list to second dropdown list
Hi Does soneone know hot to get value from first dropdown list to second dropdown list or remove it from seond dropdown list? I have a web form page. On the page, there are two dropdown lists, one Add button, and one Remove button . First dropdown list has four list items. Second one does not have list item yet. When select a item from first dropdown list, then click "add" button. I want it to apear in the second dropdown list without reload the page. Or select item from second dropdown list, and click "Remove" button. it will remove from second list. Please help. &nb...

Drop down list in Form view returns error when value in bound field (data binding
I get the following error when I click edit to edit the details in the web form:'ddlUserRestrictionType' has a SelectedValue which is invalid because it does not exist in the list of items.Parameter name: value  I would like it to accept any value which currently exists in the database. Is this easily achievable? With what you've posted, all I can tell is that you are trying to set the SelectedValue of a ddl to a value that does not exist in the list.  It would be helpful if you can post some code and the expected values of this control.~ Timwww.bucketo...

Web resources about - How to get the list of second drop-down-list based on value selected in the first drop-down-list -

The World's Richest Tech Billionaires: Familiar Faces As Zuckerberg Drops Down List
In a year when the members of Forbes Billionaires List had their combined net worth rise more than 15% since 2012 to $5.4 trillion, the world's ...

Drop-down list styling
Looking at the demos, numbers 3 and 5 are my favorites.

Drop-down list styling
Looking at the demos, numbers 3 and 5 are my favorites. ∞ Read this on The Loop

Resources last updated: 2/7/2016 5:19:05 AM