In this article I will explain with an example, how to get the selected Text and Value of
DropDownList inside SelectedIndexChanged event in
ASP.Net using C# and VB.Net.
Inside the SelectedIndexChanged event, the selected Text and Value of
ASP.Net DropDownList will be displayed in
JavaScript Alert Message Box using C# and VB.Net.
HTML Markup
The following
HTML Markup consists of:
DropDownList – For selecting Fruits.
The
DropDownList specified with
OnSelectedIndexChanged event handler and
AutoPostBack property set to True.
<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>
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;
ClientScript.RegisterStartupScript(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
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
Screenshot
Demo
Downloads