cusoxty
07-16-2004, 08: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?
<head>
<title>Account Transfers</title>
<script language="javascript" src="FormFunctions.js"></script>
</head>
<asp:datagrid id="dg_approval" runat="server" AutoGenerateColumns="False" DataKeyField="dgcounter"
Font-Names="Verdana" Font-Size="xx-small" enableviewstate="true">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<input id="chkAllItems" type="checkbox" onclick="CheckAllDataGridCheckBoxes('chk',
document.forms[0].chkAllItems.checked)" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="chk" runat="server" Text =<%# DataBinder.Eval(Container.DataItem,
"AccountID") %> checked=<%# DataBinder.Eval(Container.DataItem, "Approved") %> enableviewstate="true"/>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<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>
</Columns>
</asp:datagrid>
And here is the javascript file, FormFunctions.js.
<SCRIPT LANGUAGE="JavaScript">
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
}
}
}
}
</script>
<head>
<title>Account Transfers</title>
<script language="javascript" src="FormFunctions.js"></script>
</head>
<asp:datagrid id="dg_approval" runat="server" AutoGenerateColumns="False" DataKeyField="dgcounter"
Font-Names="Verdana" Font-Size="xx-small" enableviewstate="true">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<input id="chkAllItems" type="checkbox" onclick="CheckAllDataGridCheckBoxes('chk',
document.forms[0].chkAllItems.checked)" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="chk" runat="server" Text =<%# DataBinder.Eval(Container.DataItem,
"AccountID") %> checked=<%# DataBinder.Eval(Container.DataItem, "Approved") %> enableviewstate="true"/>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<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>
</Columns>
</asp:datagrid>
And here is the javascript file, FormFunctions.js.
<SCRIPT LANGUAGE="JavaScript">
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
}
}
}
}
</script>