Some times we need to get the selected item text and index of the drop down in asp.net using client script.I faced a problem in getting this two values using jQuery . At last i got the solution.So i thought to share this information to the learner's of jQuery.Please see this example to understand clearly.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CascadingDropDown.aspx.cs"
Inherits="CascadingExample.CascadingDropDown" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Cascading DropDownLists With ASP.NET and jQuery</title>
<style type="text/css">
.ddl_style
{
text-align: justify;
font-family: Verdana;
}
.para_Style
{
text-align: justify;
}
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var ddl = $("select[name$=ddlCountries]");
ddl.bind("change keyup", function () {
if ($(this).val() != "-1") {
alert("Index:"+ $("select[name$=ddlCountries] option:selected").val()+"</br> Selected Text:"+$("select[name$=ddlCountries] option:selected").text());
} else {
alert("Index:"+ $("select[name$=ddlCountries] option:selected").val()+"</br> Selected Text:"+$("select[name$=ddlCountries] option:selected").text());
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlCountries" runat="server" AppendDataBoundItems="true" CssClass="ddl_style">
<asp:ListItem Text="(Please Select)" Value="-1" Selected="True" />
<asp:ListItem Text="INDIA" Value="0"/>
<asp:ListItem Text="USA" Value="1"/>
<asp:ListItem Text="JAPAN" Value="2"/>
</asp:DropDownList>
</div>
</form>
</body>
</html>
If you feel any problem just ask me.
Happy Coding :-)