How to use the DropDownList's SelectedIndexChanged event

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

HTML

<asp:DropDownList ID="ddlCity" runat="server" AutoPostBack = "true" OnSelectedIndexChanged = "OnSelectedIndexChanged">
    <asp:ListItem Text="Delhi" Value="1" />
    <asp:ListItem Text="Mumbai" Value="2" />
    <asp:ListItem Text="Noida" Value="3" />
</asp:DropDownList>

C#

protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
    string message = ddlCity.SelectedItem.Text + " - " + ddlCity.SelectedItem.Value;
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}

2020-02-13