Showing posts with label jQuery Checkbox. Show all posts
Showing posts with label jQuery Checkbox. Show all posts

jQuery check uncheck all checkboxes in gridview

In this post we will see how to check and uncheck checkboxes in the gridview. We will check and uncheck checkboxes at client side in gridview using jquery. First we will add check boxes to gridview like:

Gridview with checkboxes

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderWidth="1px"
      CellPadding="3" BorderStyle="None" Font-Names="Arial">
      <FooterStyle></FooterStyle>
      <PagerStyle HorizontalAlign="Left"></PagerStyle>
      <HeaderStyle Font-Bold="True"></HeaderStyle>
      <Columns>
        <asp:TemplateField HeaderText="Select">
          <HeaderTemplate>
            <asp:CheckBox ID="checkAll" runat="server" Text="" onclick="javascript:CheckUnCheckAll(this);" />
          </HeaderTemplate>
          <ItemTemplate>
            <asp:CheckBox ID="CheckBoxPurchase" runat="server" Enabled="true" />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="Item No." DataField="ItemNo">
          <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField HeaderText="Item Name" DataField="ItemName">
          <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
        </asp:BoundField>
      </Columns>
    </asp:GridView>

Here in above code we have gridview with one checkbox in header and checkboxes for all the rows. Now we have to check all the checkboxes in gridview rows when header checkbox is checked and when header checkbox is unchecked all checkbox get unchecked. Now we have to access gridview object in jquery.

$('#<%=GridView1.ClientID %>')

In order to get and perform check-uncheck operation on checkboxes we have to call Javascript function from header checkbox of the gridview and in that function we will find all row checkboxes like:

 $('#<%=GridView1.ClientID %>').find("input:checkbox")

Now by iterating through each checkbox item we can assign header checkbox's checked status to all other checkboxes.

Iterate through gridview checkboxes to check uncheck checkboxes

<script type="text/javascript">
  function CheckUnCheckAll(chk) {
     $('#<%=GridView1.ClientID %>').find("input:checkbox").each(function () {
          if (this != chk) {
              this.checked = chk.checked;
            }
           });
      }
 </script>

Example to check-uncheck all checkboxes in gridview

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
      <title>Check all checkboxes in gridview using jQuery</title>
    <script type="text/javascript">
        function CheckUnCheckAll(chk) {
            $('#<%=GridView1.ClientID %>').find("input:checkbox").each(function () {
                if (this != chk) {
                    this.checked = chk.checked;
                }
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderWidth="1px"
            CellPadding="3" BorderStyle="None" Font-Names="Arial">
            <FooterStyle></FooterStyle>
            <PagerStyle HorizontalAlign="Left"></PagerStyle>
            <HeaderStyle Font-Bold="True"></HeaderStyle>
            <Columns>
                <asp:TemplateField HeaderText="Select">
                    <HeaderTemplate>
                        <asp:CheckBox ID="checkAll" runat="server" Text="" onclick="javascript:CheckUnCheckAll(this);" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBoxPurchase" runat="server" Enabled="true" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Item No." DataField="ItemNo">
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField HeaderText="Item Name" DataField="ItemName">
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                </asp:BoundField>
            </Columns>
        </asp:GridView>
        <input type="button" id="submit" value="Submit" />
    </div>
    </form>
</body>
</html>

Get gridview checkbox value jQuery

get gridview selected checkboxes in jquery
In asp.net gridview we show checkboxes to select rows. Some times we need to get checked checkboxes in jQuery to perform some client side operations. in jQuery we need to first get object of gridview and then need to find checked checkboxes inside gridview.

Example to get gridview checked checkboxes in jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js"></script>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/i18n/jquery-ui-i18n.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/ui-lightness/jquery-ui.css" type="text/css"/>
    <title>Get checkboxes in gridview using jQuery</title>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#submit").click(function () {
               var $checkedCheckBoxes = $('#<%=GridView1.ClientID %>').find("input:checkbox:checked");
            });
        });
      
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderWidth="1px"
            CellPadding="3" BorderStyle="None" Font-Names="Arial">
            <FooterStyle></FooterStyle>
            <PagerStyle HorizontalAlign="Left"></PagerStyle>
            <HeaderStyle Font-Bold="True"></HeaderStyle>
            <Columns>
                <asp:TemplateField HeaderText="Select">
                   <ItemTemplate>
                        <asp:CheckBox ID="CheckBoxPurchase" runat="server" Enabled="true" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Item No." DataField="ItemNo">
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField HeaderText="Item Name" DataField="ItemName">
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                </asp:BoundField>
            </Columns>
        </asp:GridView>
        <input type="button" id="submit" value="Submit"/>
    </div>
    </form>
</body>
</html>

Get checked checkboxes in jQuery

get checked checkboxes in jquery


In this post we are going to see how to get all the checked checkboxes using jQuery. Some time in asp.net applications we need to do operations at client side on controls like checkboxes, dropdown list, buttons…etc. Here we will see how to access checkboxes at client side using jQuery and how to get checked checkboxes. To get the checked checkboxes, jQuery provides selector :checked.

Sample code to get all checked checkboxes[jQuery – ASP.Net]

<input value="1" checked type="checkbox" name="numbers" />
<input value="2" type="checkbox" name="numbers" />
<input value="3" checked type="checkbox" name="numbers" />

Now to get only the checked checkboxes we will use below line of code that will store all the checked checkboxes as an array.


$checkedCheckboxes = $("input:checkbox[name=numbers]:checked");;

We can iterate though the checked checkbox array using .each and perform some operations or can get or set values to checkboxes.


var selectedValues="";
$checkedCheckboxes.each(function () {
selectedValues +=  $(this).val() +",";
});
);

Let's put all above code togather

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<title></title>
<script type="text/javascript">
$(document).ready(function () {
var selectedValues="";
$checkedCheckboxes = $("input:checkbox[name=numbers]:checked");
$checkedCheckboxes.each(function () {
selectedValues +=  $(this).val() +",";
});
alert(selectedValues);
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input value="1" checked type="checkbox" name="numbers" />
<input value="2" type="checkbox" name="numbers" />
<input value="3" checked type="checkbox" name="numbers" />
</div>
</form>
</body>
</html>

//Output
//1,3

using jquery