Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > asp.net > december 2007 >

asp.net : Dynamic asp:DrownDownList control


Bob
12/6/2007 3:02:00 PM
I have a static AutoPostBack asp:DrownDownList and depending on the selected
content of this list, I want to either 1) not do anything or 2) generate and
populate a new asp:DrownDownList below it. How do I do this on the server
side? I am trying to do this with minimal client-side JavaScript being
generated.

Thanks

Jacques
12/6/2007 10:53:00 PM
If I'm understanding you correctly, we're talking about the following
hypothetical scenario: You have a country list and when the user makes a
selection you either do nothing or you load a list of States/Regions
depending on the selection made?

If that scenario is near enough, you could consider the following approach:
At design time, create the second dropdownlist where ever it should be and
set it's visibility to false, or if you don't mind the user seeing it, set
it's enabled property to false.

Capture the Country list's SelectedIndexChanged event and based on your
conditions either load the second list or do nothing.

eg.
protected void Countries_SelectedIndexChanged(....,....)
{
if (Countries.SelectedIndex > 0)
{
Regions.DataSource =
BizLayer.GetRegions(Convert.ToInt16(Countires.SelectedValue));
Regions.DataTextField = "textField";
Regions.DataValueField = "valueField";
Regions.DataBind();
}
}

Make sure your viewstate is enabled for these lists else you may not get the
expected result.

Hope this helped.
Cheers
Jacques

[quoted text, click to view]
Bob
12/6/2007 11:44:58 PM
Thanks for your response. However on the internet I found exactly what I
was looking for in the following article:

http://aspnet.4guysfromrolla.com/articles/081402-1.aspx

Thanks again.

David C
12/7/2007 11:01:12 AM
Have you looked at ASP.Net AJAX? There is a control already built that does
this.

David
[quoted text, click to view]

AddThis Social Bookmark Button