View Full Version : Check/Uncheck all in Datagrid

07-16-2004, 09:46 PM
Here is code that should create a header with a checkbox in it. I want this checkbox to select or unselct all checkboxes in the grid depending on if its checked or not. Right now, nothing happens when you click this check box. any suggestions?


<title>Account Transfers</title>

<script language="javascript" src="FormFunctions.js"></script>


<asp:datagrid id="dg_approval" runat="server" AutoGenerateColumns="False" DataKeyField="dgcounter"

Font-Names="Verdana" Font-Size="xx-small" enableviewstate="true">




<input id="chkAllItems" type="checkbox" onclick="CheckAllDataGridCheckBoxes('chk',

document.forms[0].chkAllItems.checked)" />



<asp:CheckBox id="chk" runat="server" Text =<%# DataBinder.Eval(Container.DataItem,

"AccountID") %> checked=<%# DataBinder.Eval(Container.DataItem, "Approved") %> enableviewstate="true"/>




<asp:BoundColumn DataField="dgcounter" visible="false"></asp:BoundColumn>

<asp:BoundColumn DataField="accountname" HeaderText="Account Name"></asp:BoundColumn>

<asp:BoundColumn DataField="TerritoryName" HeaderText="From Territory"></asp:BoundColumn>

<asp:BoundColumn DataField="Expr1" HeaderText="To Territory"></asp:BoundColumn>

<asp:BoundColumn DataField="Explanation" HeaderText="Explanation"></asp:BoundColumn>

<asp:BoundColumn DataField="effectivedate" HeaderText="Effective Date"></asp:BoundColumn>

<asp:BoundColumn DataField="approved" HeaderText="Approved"></asp:BoundColumn>



And here is the javascript file, FormFunctions.js.


function CheckAllDataGridCheckBoxes(aspCheckBoxID, checkVal) {

re = new RegExp(':' + aspCheckBoxID + '$') //generated control

name starts with a colon

for(i = 0; i < document.forms[0].elements.length; i++) {

elm = document.forms[0].elements[i]

if (elm.type == 'checkbox') {

if (re.test(elm.name)) {

elm.checked = checkVal






07-19-2004, 10:38 PM
Not sure if you figured this out yet but I would try an alert in the js file to make sure the regex is matching the form field.