In this article I will explain with an example, how to use SelectedIndexChanged event of DropDownList placed inside AJAX UpdatePanel in ASP.Net using C# and VB.Net.
The AsyncPostBackTrigger needs to be specified in the AJAX UpdatePanel in order to fire the SelectedIndexChanged event of the DropDownList using Partial PostBack.
The SelectedIndexChanged event will work / fire / trigger only when the AutoPostBack property of the ASP.Net DropDownList is set to True.
HTML Markup
The HTML Markup consists of an ASP.Net ScriptManager and a DropDownList placed inside AJAX UpdatePanel.
The ASP.Net DropDownList has been specified with OnSelectedIndexChanged event handler and AutoPostBack property set to True.
The AsyncPostBackTrigger is specified in the AJAX UpdatePanel in order to fire the SelectedIndexChanged event of the DropDownList using Partial PostBack.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlFruits" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnSelectedIndexChanged">
<asp:ListItem Text="Mango" Value="1" />
<asp:ListItem Text="Apple" Value="2" />
<asp:ListItem Text="Banana" Value="3" />
<asp:ListItem Text="Guava" Value="4" />
<asp:ListItem Text="Orange" Value="5" />
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlFruits" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
The SelectedIndexChanged event of ASP.Net DropDownList
When an item is changed in ASP.Net DropDownList, the following OnSelectedIndexChanged event handler is executed.
Inside the event handler, the selected Text and Value of the DropDownList Selected Item is fetched and displayed using JavaScript Alert message box.
C#
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
string message = ddlFruits.SelectedItem.Text + " - " + ddlFruits.SelectedItem.Value;
ScriptManager.RegisterStartupScript((sender as Control), this.GetType(), "alert", "alert('" + message + "');", true);
}
VB.Net
Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
Dim message As String = ddlFruits.SelectedItem.Text & " - " & ddlFruits.SelectedItem.Value
ScriptManager.RegisterStartupScript(CType(sender, Control), Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
Screenshot
Demo
Downloads