I have this asp net drp box:
<asp:DropDownList ID="ddlLayersList"
runat="server"
BackColor="#FFFFC0"
CssClass="form-control fullwidth" OnTextChanged="ddlLayersList_SelectedIndexChanged" >
</asp:DropDownList>
Here is how I fill the drop box:
private void SetLayers(Dictionary<string, string> layers)
{
ddlLayersList.DataSource = layers;
ddlLayersList.DataValueField = "Key";
ddlLayersList.DataTextField = "Value";
ddlLayersList.DataBind();
ddlLayersList.Items.Insert(0, "-Select Item-");
}
And here is code behind event:
protected void ddlLayersList_SelectedIndexChanged(object sender, EventArgs e)
{
}
When I select item from drop box the event not fired.
Any idea why event not fired when I change selection on DropDownList?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You need to set the AutoPostBack property to True.
<asp:DropDownList ID="ddlLayersList" AutoPostBack="True">
The value of this property:
true if a postback to the server automatically occurs whenever the
user changes the selection of the list; otherwise, false. The default
is false
Also you need to set the OnSelectedIndexChanged event instead of OnTextChanged:
<asp:DropDownList ID="ddlLayersList"
AutoPostBack="true" runat="server"
OnSelectedIndexChanged="ddlLayersList_SelectedIndexChanged">
Method 2
Besides on setting AutoPostBack="true" to enable automatic postback, also set OnSelectedIndexChanged event handler instead of OnTextChanged (note that both of them are not the same event):
<asp:DropDownList ID="ddlLayersList"
runat="server"
BackColor="#FFFFC0"
AutoPostBack="true"
CssClass="form-control fullwidth"
OnSelectedIndexChanged="ddlLayersList_SelectedIndexChanged">
</asp:DropDownList>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0