Reputation: 3578
How to make a dynamic dropdownlist on asp.net?
I have a dropdownlist:
<asp:DropDownList ID="FirstParameter" runat="server">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
</asp:DropDownList>
A label and the other dropdownlist:
<asp:Label ID="SecondParameter_Label" runat="server" Text=""></asp:Label>
<asp:DropDownList ID="SecondParameter" runat="server">
</asp:DropDownList>
When I change the selection on the FirstParameter
dropdownlist, then the text on SecondParameter_Label
should change based on the selection, and so for the SecondParameter
dropdownlist, should be binding with the data filter based on the selected value on the first dropdownlist.
I have tried this code but it doesn't work:
Protected Sub FirstParameter_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles FirstParameter.SelectedIndexChanged
'Change the label text
SecondParameter_Label.Text = ReportType_Dropdownlist.SelectedItem.Value
'Bind value to the SecondParameter dropdownlist
SecondParameter.DataSource = dataTableName '--> Assumed that I already have the DataTable called from a class
SecondParameter.DataTextField = "NameofDatabaseColumn"
SecondParameter.DataValueField = "NameofDatabaseColumn"
SecondParameter.DataBind()
End Sub
I have tried to change the SecondParameter_Label
text first but still doesn't work. How can I do that?
Upvotes: 0
Views: 1698
Reputation: 3343
Try adding AutoPostBack property to the dropdowns. You will also need to handle them on the server side. Your first dropdown should like this:
<asp:DropDownList ID="FirstParameter" runat="server" AutoPostBack="true">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
</asp:DropDownList>
Upvotes: 1