Change dropdown list values dynamically?

Mike521

Freshman
Joined
May 3, 2004
Messages
27
Hey all,

I have 2 drop down lists on a form. The values of list 2 need to be based on what the user chooses in list 1.

Is it possible to change these values on the fly? Fill it with certain data at runtime, then when the user makes a choice on list 1, modify the values of list 2 appropriately?

Furthermore, if they then go back and change their choice for list 1, I'd like to set list 2 to the default value.

So for example, when list 1 is clicked, it will auto post back. On postback, list 2 will be populated based on what list 1 says, and it will be set to it's default value (something like "select one").


Can anyone tell me if this is possible, and any problems I might run into? Thanks in advance :)
 
Cassio said:
Just set the autopostback property on the dropdownlist 1 to true and on its SelectedIndexChanged populate the dropdownlist 2.

right but how do I populate it? what is the property that I modify?
 
maybe hold the default value of list2 in a variable, then when list1 is changed, assign list2 to the variable...

maybe something like this;

list2.selecteditem.value = defaultvalue

but look into these:

ddl.SelectedItem.Selected
or
ddl.Items.FindByText(strSelectedType2).Selected
 
Cassio said:
How did you originally populate your dropdownslist 2?


like so:

Code:
<asp:DropDownList id="Children" runat="server" EnableViewState="false" AutoPostBack="true">
  <asp:ListItem>Select One</asp:ListItem>
  <asp:ListItem>1 Child - $135.00 Per Session</asp:ListItem>
  <asp:ListItem>2 Children - $209.00 Per Session</asp:ListItem>
  <asp:ListItem>3 Children - $275.00 Per Session</asp:ListItem>
</asp:DropDownList>

so I figured I'd try to mess with the "listitem" property of the drop down list, but I didn't see one pop up automatically as I typed. I'm using dreamweaver rather than visual studio.net, so maybe there are properties dreamweaver is unaware of..?

or perhaps I can give each individual list item a name, and then change the listitem.text property somehow?
 
On the codebehind page you can do something like this
Code:
Private Sub Parent_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Parent.SelectedIndexChanged

If Parent.SelectedItem.Value = x Then
[INDENT] Children.Items.Add(new ListItem("Select Value",0))[/INDENT] 
[INDENT] Children.Items.Add(new ListItem("1 Child - $135.00 Per Session",1))[/INDENT] 
[INDENT] Children.Items.Add(new ListItem("2 Children - $209.00 Per Session",2))[/INDENT] 
[INDENT] Children.Items.Add(new ListItem("3 Children - $275.00 Per Session",3))[/INDENT] 
[INDENT] Children.SelectedIndex = 0[/INDENT] 
End If

End Sub
 
Cassio said:
On the codebehind page you can do something like this
Code:
Private Sub Parent_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Parent.SelectedIndexChanged

If Parent.SelectedItem.Value = x Then
[INDENT] Children.Items.Add(new ListItem("Select Value",0))[/INDENT] 
[INDENT] Children.Items.Add(new ListItem("1 Child - $135.00 Per Session",1))[/INDENT] 
[INDENT] Children.Items.Add(new ListItem("2 Children - $209.00 Per Session",2))[/INDENT] 
[INDENT] Children.Items.Add(new ListItem("3 Children - $275.00 Per Session",3))[/INDENT] 
[INDENT] Children.SelectedIndex = 0[/INDENT] 
End If

End Sub


thanks Cassio that looks great! However I threw it in and made the proper modifications and it's giving me an error:

edit: had a problem but that's fixed, now I have a new one:

every time the user makes a change, it adds the new values over and over again! Any way to first blank out the contents of the drop down list, then rebuild them?
 
Last edited:
Mike521 said:
thanks Cassio that looks great! However I threw it in and made the proper modifications and it's giving me an error:

edit: had a problem but that's fixed, now I have a new one:

every time the user makes a change, it adds the new values over and over again! Any way to first blank out the contents of the drop down list, then rebuild them?

lstYourListName.Items.Clear b4 populating the list
 
Please do remember if you do in this way,
it means every time list 1 value change,
the page will refresh.

Please imagine if user use keyboard to select your list 1 value,
terrible...

For me I will move this process to client side rather than server side,
mean use Javascript to handle it
 
Back
Top