Breaking News

Editors Picks

Thursday, December 15, 2011

check uncheck checkbox in a gridview using javascript


<script type="text/javascript">
 function SelectAllCheckboxes(cbSelectAll)
 {
var gvcheck = document.getElementById('gvuseredit');
var i;
if (cbSelectAll.checked)
{
for (i = 0; i < gvcheck.rows.length-1; i++)
{
var inputs = gvcheck.rows[i].getElementsByTagName('input');
inputs[0].checked = true;
}
      }
      else
      {
            for (i = 0; i < gvcheck.rows.length-1; i++)
{
var inputs = gvcheck.rows[i].getElementsByTagName('input');
inputs[0].checked = false;
}
      }
}
function Selectchildcheckboxes(header)
{
var ck = header;
var count = 0;
var gvcheck = document.getElementById('gvuseredit');
var headerchk = document.getElementById(header);
var rowcount = gvcheck.rows.length;
for (i = 1; i < gvcheck.rows.length-1; i++)
{
var inputs = gvcheck.rows[i].getElementsByTagName('input');
if (inputs[0].checked)
 {
  count++;
       }
       }
if (count == rowcount-1)
{
 cbSelectAll.checked = true;
}
else
{
 cbSelectAll.checked = false;
}
}
script>




<asp:GridView ID="gvuseredit" runat="server" AllowSorting="true" AutoGenerateColumns="false"
AllowPaging="true" ShowFooter="true"
onrowdatabound="gvuseredit_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Select User">
<HeaderTemplate>
<asp:CheckBox ID="cbSelectAll" runat="server" onclick="javascript:SelectAllCheckboxes(this)" Text="Select All" />
HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkbdeleteuser" runat="server" />
ItemTemplate>
<FooterTemplate>
<asp:Button ID="btndeleteUser" runat="server" Text="Delete" />
FooterTemplate>
<HeaderStyle Width="" />
asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:LinkButton ID="lbkUpdate" runat="server" CausesValidation="True" CommandName="Update"
Text="Update">asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">asp:LinkButton>
EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit">asp:LinkButton>
ItemTemplate>
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" />
asp:TemplateField>
<asp:TemplateField HeaderText="rec ID">
<ItemTemplate>
<asp:Label ID="lblrecID" runat="server" Text='<%# Bind("recID") %>'>asp:Label>
ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtrecID" runat="server" Text='<%# Bind("recID") %>'>asp:Label>
EditItemTemplate>
<HeaderStyle Width="" />
asp:TemplateField>
<asp:TemplateField HeaderText="User Name">
<ItemTemplate>
<asp:Label ID="lblUserName" runat="server" Text='<%# Bind("UserName") %>'>asp:Label>
ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblUserName" runat="server" Text='<%# Bind("UserName") %>'>asp:Label>
EditItemTemplate>
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" />
asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label ID="lblpwd" runat="server" Text='<%# Bind("pwd") %>'>asp:Label>
ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lbpwd" runat="server" Text='<%# Bind("pwd") %>'>asp:Label>
EditItemTemplate>
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" />
asp:TemplateField>
Columns>
asp:GridView>





protected void gvuseredit_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox headerchk =    (CheckBox)gvuseredit.HeaderRow.FindControl("cbSelectAll");
CheckBox childchk = (CheckBox)e.Row.FindControl("chkbdeleteuser");
childchk.Attributes.Add("onclick", "javascript:Selectchildcheckboxes('" + ((System.Web.UI.Control)(headerchk)).ClientID + "')");
}
}

No comments :

Post a Comment

Contact Us

Name

Email *

Message *