Monday, 18 March 2013

how to checked all the cheboxes in grideview using javascript

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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></title>
    <script language="javascript" type="text/javascript">
   function z() {
            var m = document.getElementById("<%= GridViewApprovePos.ClientID %>");
            var q = m.getElementsByTagName("input");
            var n = m.getElementsByTagName("tr");
            var p = m.getElementsByTagName("td");
            var k = 3;
            for (var i = 1; i < n.length; i++) {

                for (var j = 1; j < q.length; j++)
                {
                   // if (q[l].type == "checkbox")
                    //if(q[l].checked)
                    if (q[j].type == "text")
                        if (q[j].value != NaN) {
                            alert(p[k].innerHTML);
                            break;
                        }

                }
                k = k + 6;
            }

        }
        function x()
         {
            var m = document.getElementById('<%=gv.ClientID %>');
            var n = m.getElementsByTagName("input");
            if (n[0].checked)
                for (var i = 1; i < n.length; i++)
                    n[i].checked = true;
            else
                for (var i = 1; i < n.length; i++)
                    n[i].checked = false;
        }
        function y() {
            var m = document.getElementById('<%=gv.ClientID %>');
            var n = m.getElementsByTagName("input");
            var k=0;
            for (var i = 1; i < n.length; i++)
            if(n[i].checked)
                    k = k + 1;
            var p = parseInt(n.length) - 1;
            if (k == p)
                n[0].checked = true;
            else
                n[0].checked = false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <center>
     <asp:GridView ID="gv" AutoGenerateColumns="false"  runat="server">
    <Columns>
    <asp:TemplateField>
    <HeaderTemplate>
    <asp:CheckBox ID="hcb" runat="server" onclick="x()" />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="cb" runat="server" onclick="y()" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField HeaderText="ID" DataField="ID" />
      <asp:BoundField HeaderText="NAME" DataField="NAME" />
      <asp:BoundField HeaderText="DEPT" DataField="DEPT" />
    </Columns>
    </asp:GridView>
    </center>
    </div>
    </form>
</body>
</html>

No comments:

Post a Comment