Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Set Selected Value on Multiple Dropdowns - which depend on each other

Posted on 2006-07-18
Medium Priority
Last Modified: 2012-06-22
I am pulling data out of a database into a form for en 'edit' administrative section.

I select my data etc..
I populate my textboxes just fine.

There is one textbox in particular 'zip_code' which is what another 'county' dropdown(sqldatasource) rely on for its values

Then there is a 'city' dropdown whos datasource depends on the value of 'county' for its values.

For the edit page workflow, id like the default values on page load (not postback) to populate with the values that have been written into the database.

so - i have tried looping throuh the controls and tetting the textbox.text values or dropdown.selectedvalue = datareader value

I get as far as county, but then city _will not_ auto select.
My best guess on this is that the sqldatasource webcontrols do thier stuff _after_ the Sub Page_Load?  which is causing my problems?
Im probably wrong though..
Anyone have any idea how i can resolve this with out digging too deep into codebehinds?
Id rather not have to build dropdown lists by hand every time the page loads.
(example, user edits the zip_code and now a whole new set of issues arrises)

an ideal solution is one where the datasource webcontrols cause the dropdowns to 'just work' and enable viewstate keeps selected values across postbacks as long as the zip code doesnt change, in which case they select new values anyways (and it keeps them)


Question by:EchoBinary
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
LVL 96

Expert Comment

by:Bob Learned
ID: 17133516
There is always the Atlas ToolKit and the Cascading DropDown Control:

The "Atlas" Control Toolkit helps you bring your websites to life!


Author Comment

ID: 17133645
Awesome site! however not something i can use right now.
I will look into that for the future.    any further advice?
LVL 96

Expert Comment

by:Bob Learned
ID: 17133705
Do you want to do all this without post-backs?

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 17133719
i dont mind the postbacks at the moment  - but id like it to work as described
v2.0 will b w/o postbacks  

deadlines..  you know  ;)
LVL 25

Expert Comment

ID: 17133760
No need to loop through the controls. Make sure that you use a If(!Page.IsPostBack) before you call your binding routine in page_load event:

private Page_Load(object src, EventArgs e)
  if (!Page.IsPostBack)

I am assuming you have a separate editable section for your records. If it is so, your workflow should look like this.

1. Connect to database

2. Get Records

3. Populate the value for the parent field. If it is a listbox or drop down that has several values already populated, then seet its selected index as follows:

MyListBox.SelectedIndex = MyListBox.Items.IndexOf(MyListBOx.Items.FindByValue("PARENT_FIELD_VALYE"));

Once you have set the selected index, it will stay selected between post-back as long as user do not select a different value.

4. Depending on the value of parent field, populate the value of child field.

5. Populate another parent field.

6. Depending on the value of parent field, populate the value of child field.

and so on. If you have a text-field like zip-code field that is a parent for another field, you can write an evenhandler for TextChanged event and then bind the approprite child control.

HTH, Nauman.

Author Comment

ID: 17271818
Upon further investigation i am forced to wonder if there is an order of operations issue going on.

The code that tells the dropdown which to select by default is in (obviously) a code behind.
The dropdowns are databound to asp controls on the page itself. is it poss. that the databinding is happening _after_ the codebehind gets run?
LVL 25

Accepted Solution

nauman_ahmed earned 2000 total points
ID: 17271930
Yes. Databinding for dependent text-box is mostly handled when the selected index is changed for parent drop-down. Check the SelectedIndexChanged event for the parent drop-down to see if it has additional code.


Author Comment

ID: 17437670
My solution was to realize what the order of operations is/was for the page. as such ill give pts to nauman for answering that part

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question