aspx Tutorial

.NET Articles,jQuery demo, asp.net with jQuery, online tutorial,Jquery, SilverLight, Javascript, asp.net,JSON, MVC,.NET Articles,demo, Web Services, .NET articles, Sharepoint 2010, visual studio 2010,Aamir Hasan,IT, Building Your First Web Application Project
Advertise Here

Toolbar

Get our toolbar!

Advertize



Posted by Aamir Hasan   on Friday, July 22, 2011 Total Views:  

In this post, we will see how to get total items from DropDownList using jQuery. We will use the button to display the total items in the Label.

Let’s say we have DropDownList like this:

        <asp:DropDownList ID="DropDownList1" runat="server">
           
<asp:ListItem Value="0">SELECT</asp:ListItem>
           
<asp:ListItem Value="1">APPLE</asp:ListItem>
           
<asp:ListItem Value="2">BANANA</asp:ListItem>
           
<asp:ListItem Value="3">ORANGE</asp:ListItem>
           
<asp:ListItem Value="4">MANGO</asp:ListItem>
       
</asp:DropDownList>

 

To find out the total items when button is clicked and display in the Label, do this:

        <input id="btnClick" type="button" value=" Click " />
       
<asp:Label ID="Label1" runat="server"></asp:Label>

 

Here is the code used to get the total items from DropDownList and display in the Label when button will be clicked.

<script type="text/javascript">

        $(function () {
            $("#btnClick").click(function () {
                var len = $("#<%=DropDownList1.ClientID %> option").length;
                $("#<%=Label1.ClientID %>").text("Total items: " + len);
            });
        });

</script>

 


Here’s an example

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js" type="text/javascript"></script>

    <script type="text/javascript">

        $(function () {

            $("#btnClick").click(function () {

                var len = $("#<%=DropDownList1.ClientID %> option").length;

                $("#<%=Label1.ClientID %>").text("Total items: " + len);

            });

        });

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:DropDownList ID="DropDownList1" runat="server">

            <asp:ListItem Value="0">SELECT</asp:ListItem>

            <asp:ListItem Value="1">APPLE</asp:ListItem>

            <asp:ListItem Value="2">BANANA</asp:ListItem>

            <asp:ListItem Value="3">ORANGE</asp:ListItem>

            <asp:ListItem Value="4">MANGO</asp:ListItem>

        </asp:DropDownList>

        <br />

        <input id="btnClick" type="button" value=" Click " />

        <asp:Label ID="Label1" runat="server"></asp:Label>

    </div>

    </form>

</body>

</html>


Output

Download

DropDownList-total-items-count.rar (673.00 bytes)

See live demo

Category: All | asp.net | Control
Protected by Copyscape Online Plagiarism Tool

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5