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>

jQuery call page codebehind method

In asp.net application sometimes we need to call codebehind method from client script. In this post we will see how to call codebehind page method using jquery ajax.

To call codebehind page method from jquery we need to make method static. Also we need to add [WebMethod] attribute for method. So the code behind method will look like:

 [WebMethod]
  public static string GetMessage()
  {
   return "Codebehind method call...";
  }

We also need to add namespace using System.Web.Services; to add [WebMethod] attribute to code behind method.
Now we can call this codebehind method using jquery ajax like

var loc = window.location.href;
$.ajax({
         type: 'POST',
          url: loc + "/GetMessage",
          data: "{}",
          contentType: "application/json; charset=utf-8"
        
        })
        .success(function (response) {
          alert(response.d);

        })
        .error(function (response) {
          alert(response.d);
        });

Here using window.location.href we are getting url of aspx page and by appending method name to it we can call code behind method.

Below code demonstrats how to call codebehind method using jquery ajax and json

jQuery ajax call codebehind page method

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function () {
var loc = window.location.href;
$("#btnClick").click(function (event) {
$.ajax({
type: 'POST',
url: loc + "/GetMessage",
data: "{}",
contentType: "application/json; charset=utf-8"
})
.success(function (response) {
alert(response.d);
})
.error(function (response) {
alert(response.d);
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnClick" runat="server" Text="Button" />
</div>
</form>
</body>
</html>

Below is the code behind method that will get called by jquery ajax call:

using System;
using System.Web.Services;

namespace aspnetcontrol
{
  public partial class jquerycallpagemethod : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string GetMessage()
    {
      return "Codebehind method call...";
    }
  }
}

Remove Hide blogger|blogspot header navbar

Blogger or Blogspot is the easy way to write blogs. In blogger by default it shows header navbar on top. To hide blogspot|blogger navbar from header follow the below steps:
1. Login to blogger account.
2. Go to Template in new blogger view
3. Then click on Edit Html.

hide remove blogspot header navbar using css

4. Search for <b:skin>
5. Below that add css:
#navbar-iframe
{
height:0px;
visibility:hidden;
display:none;
}
This will hide or disable the blogger default header navbar.