Dynamically change the contents of a drop down list in C#.net

Gunslinger08

Lifer
Nov 18, 2001
13,234
2
81
Is it possible? For example, changing the contents of a "State" DDL based on the user changing the "Country" DDL. I don't want it to postback, so that the page doesn't refresh and leave the user back at the top of the page.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
Two possibilities come to mind -- send all the data to the browser when the page is initially loaded and then just use Javascript to switch the data in the state DDL when the country DDL changes.

Alternately, use something like the XMLHttpRequest object to place a request for the new data without doing a postback -- once you retrieve the new data, then populate the state DDL with Javascript.
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
Oog recommendations are right-on; however if you want to do it in C# (and not have to write javascript) you will have to perform a post-back. What you can do is alter the behavior of the browser between postbacks, so that the browser will post-back without looking like it has. IE only.

Wonders of the Little-Known SmartNavigation Property

Smart Navigation is a little-known Internet Explorer feature that enables the individual controls on your Web forms to maintain focus between post backs, plus allows you to suppress that "flicker" that occurs as you load the new page.

To turn on this little-known feature, simply set the SmartNavigation property of your ASPX page to True. Note that Smart Navigation only works on Internet Explorer 5 and above; however, the .NET Framework will automatically detect this and only serve up the "smart" code if the target browser supports it.