Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2004
    Thanked 0 Times in 0 Posts

    Check/Uncheck all in Datagrid

    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.

    <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

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Cincinnati, OH
    Thanked 0 Times in 0 Posts
    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.
    does this sig match?


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts