Dynamic asp:DrownDownList control

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

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
 
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
 
Have you looked at ASP.Net AJAX? There is a control already built that does
this.

David
 
Back
Top