Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    36
    Thanks
    0
    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?

    Code:
    <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.


    Code:
    <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>

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Cincinnati, OH
    Posts
    545
    Thanks
    0
    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
    •