Hi kankon,
Please refer sample below.
HTML
<asp:DropDownList ID="ddlCities" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnSelectedIndexChanged">
<asp:ListItem Text="" Value="0"></asp:ListItem>
<asp:ListItem Text="Mumbai" Value="1" Selected="True"></asp:ListItem>
<asp:ListItem Text="Delhi" Value="2"></asp:ListItem>
</asp:DropDownList>
<script type="text/javascript">
function OpenAlert(message) {
alert(message);
}
</script>
Code
C#
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(ddlCities.SelectedItem.Text))
{
ClientScript.RegisterStartupScript(this.GetType(), "message", "OpenAlert('Bad name.')", true);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "message", "OpenAlert('Selected city " + ddlCities.SelectedItem.Text + ".')", true);
}
}
VB.Net
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If String.IsNullOrEmpty(ddlCities.SelectedItem.Text) Then
ClientScript.RegisterStartupScript(Me.GetType(), "message", "OpenAlert('Bad name.')", True)
Else
ClientScript.RegisterStartupScript(Me.GetType(), "message", "OpenAlert('Selected city " & ddlCities.SelectedItem.Text & ".')", True)
End If
End Sub
Screenshot