SIGN UP MEMBER LOGIN:    
Resource

Get the Dropdown Selected Index and Text

Hemant Kumar Resources Nov 12, 2011
This is resource is meant for getting the selected text and index using jQuery in ASP.NET.
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 :-)
share this resource :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Sponsored by
Become a Sponsor