menu

Thursday, July 7, 2011

Check All Check box in Grid View Control without Postback

JavaScript

<script type="text/javascript" language="javascript">

function checkAllBoxes(grid, num)

{

var totalChkBoxes = parseInt(num);

var gvControl = document.getElementById(grid);

var gvChkBoxControl = "chkBoxChild";

var mainChkBox = document.getElementById("chkBoxAll");

var inputTypes = gvControl.getElementsByTagName("input");

for(var i = 0; i < inputTypes.length; i++)

{

if(inputTypes[i].type == 'checkbox' && inputTypes[i].id.indexOf(gvChkBoxControl,0) >= 0)

inputTypes[i].checked = mainChkBox.checked;

}

}

</script>

Grid View Control

<asp:GridView ID="grdDR" runat="server">

<Columns>

<asp:TemplateField>

<HeaderTemplate>

<input id="chkBoxAll" type="checkbox"

onclick="checkAllBoxes('<%= grdDR.ClientID %>', '<%= grdDR.Rows.Count %>')" />

</HeaderTemplate>

<ItemTemplate>

<asp:CheckBox ID="chkBoxChild" runat="server" />

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField DataField="Trns ID" HeaderText="Trns ID" >

<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />

</asp:BoundField>

<asp:BoundField DataField="User" HeaderText="User" >

<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />

</asp:BoundField>

<asp:BoundField DataField="Deposit Amount" HeaderText="Deposit Amount" >

<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />

</asp:BoundField>