• Twitter
  • Facebook
  • Google+
  • Instagram
  • Youtube

Monday, March 17, 2008

Check all checkbox within GridView or DataGrid in Asp.Net

Check all checkbox within GridView or DataGrid in Asp.Net
===================================================

Sometimes we need to handle functionality in our asp.net pages, like when a header
checkbox become selected, all checkbox within a GridView or DataGrid should become selected.
So here, this is the way, by it you can handle this types of functionality.

Suppose your page GridView layout is like:
-----------------------------------------------------

<asp:GridView ID="gvCompanyInvoiceList" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" onclick="CheckAll(this);" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkgdApprove" onclick="CheckChanged(this);" runat="server" />
</ItemTemplate>
<HeaderStyle Width="5%" />

</asp:TemplateField>
</Columns>
</asp:GridView>

Use this script to handle checkbox functionalities.
---------------------------------------------------------------

<script type="text/javascript">
function CheckAll(checkAllBox)
{

var frm = document.form1;
var ChkState=checkAllBox.checked;
for(i=0; i< frm.length;i++)
{
e = frm.elements[i];
if(e.type=='checkbox' && e.name.indexOf('chkgdApprove') != -1)
e.checked= ChkState ;
}
}
function CheckChanged()
{
var frm = document.form1;
var boolAllChecked;
boolAllChecked=true;
for(i=0;i< frm.length;i++)
{
e=frm.elements[i];
if ( e.type=='checkbox' && e.name.indexOf('chkgdApprove') != -1 )
if(e.checked== false)
{
boolAllChecked=false;
break;
}
}
for(i=0;i< frm.length;i++)
{
e=frm.elements[i];
if ( e.type=='checkbox' && e.name.indexOf('chkAll') != -1 )
{
if( boolAllChecked==false)
e.checked= false ;
else
e.checked= true;
break;
}
}
}
</script>

That's it !
Hope you will like it.

0 comments:

Contact

Get in touch with me


Adress/Street

12 Street West Victoria 1234 Australia

Phone number

+(12) 3456 789

Website

www.johnsmith.com