Book Image

ASP.NET jQuery Cookbook

By : Sonal Aneel Allana
Book Image

ASP.NET jQuery Cookbook

By: Sonal Aneel Allana

Overview of this book

<p>jQuery is a powerful, lightweight JavaScript library that simplifies various web development tasks. With the integration of jQuery with Visual Studio 2010, it has become increasingly popular for ASP.NET developers to use jQuery's awesome power in their applications. Combining jQuery and ASP.NET creates applications that are anything but lightweight. Using this Cookbook you will become adept at using this library in your ASP.NET web applications.</p> <p>This cookbook shares the most powerful, impressive, and just plain handy jQuery secrets for ASP.NET developers. From substitute server-side code to corresponding client script, it shows ASP.NET developers how to get the most from jQuery in a simple, effective, and easy manner.</p> <p>The cookbook takes a pragmatic approach in applying the jQuery library to ASP.NET applications. It will help you to master the use of jQuery with ASP.NET by taking you step-by-step through hands-on, practical recipes. With its help, you can learn to manipulate various ASP.NET controls like TextBox, CheckBoxList, DropDownList, BulletedList, Hyperlink, Image, and GridView as well as custom user controls. The book teaches client validation techniques thus providing a substitute for the server-side Validation Control. It explores various graphic and animation effects that can be accomplished very easily with the library to give a better interactive experience to the end user. It explores AJAX in depth and describes the use of Firebug to view / troubleshoot the corresponding request / response dialog. You will also learn to develop rich content using client scripting.</p>
Table of Contents (15 chapters)
ASP.NET jQuery Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Displaying selected items of a CheckBoxList


In this recipe, we will retrieve the selected items of an ASP.NET CheckBoxList.

Getting ready

  1. Create a new form Recipe5.aspx in the current project. Add a CheckBoxList control to the form. Also add a div area to display the selected items of the CheckBoxList at runtime:

    <form id="form1" runat="server">
         <div align="left">
            <fieldset style="width:400px;height:150px;">
           <p>Different types of packages available:</p>
            <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem Text="Standard" Value="1"></asp:ListItem>
            <asp:ListItem Text="Silver" Value="2"></asp:ListItem>
            <asp:ListItem Text="Gold" Value="3"></asp:ListItem>
            <asp:ListItem Text="Premier" Value="4"></asp:ListItem>
            </asp:CheckBoxList>
            </fieldset>
            <br />
            <div id="message"></div>
        </div>
        </form>
  2. The page will display the CheckBoxList as shown below:

How to do it…

  1. In the document.ready() function, define the click function of the CheckBoxList:

    $('#<%=CheckBoxList1.ClientID%>').click(function() {
  2. Create a local string variable and initialize to an empty string:

    var str = "";
  3. Apply a jQuery selector to retrieve the checked boxes and apply the each function to each element returned:

    $('#<%=CheckBoxList1.ClientID%> input[type=checkbox]:checked').each(function() {

    Note

    Note that at runtime, the CheckBoxList control is rendered as a container table <table id="CheckBoxList1"> and each of the ListItems are rendered as <input type="checkbox">.

  4. Retrieve the text of the selected CheckBox and append to the local string:

    str = str + " " + $(this).next().text();

    Note

    Each of the checkbox elements is rendered as a <input type="checkbox"> and <label> pair. The label holds the text of the element. Hence, when retrieving the description of the element, we have used $(this).next().text(), where $(this) refers to the current checkbox element and next() refers to the consecutive <label> element.

  5. After the execution of the each() function, display the local string in the div area 'message':

    $('#message').text(str);
    });

The complete jQuery solution is as follows:

<script type="text/javascript">
        $(document).ready(function() {
            $('#<%=CheckBoxList1.ClientID%>').click(
                function() {
                    var str = "";
                    $('#<%=CheckBoxList1.ClientID%> input[type=checkbox]:checked').each(function() {
                        str = str + " " + $(this).next().text();
                    });
                    $('#message').text(str);
                });
        });
    </script>

How it works…

Run the page. Select the required checkboxes. The page will display the text of the selected checkboxes as follows:

See also

Selecting/deselecting all items in ASP.NET CheckBoxList