0

Within a masterpage I have a standard DataGrid:

  <asp:DataGrid ID="dgMyGrid" runat="server" AutoGenerateColumns="false">
            <Columns>
            <asp:TemplateColumn>
            <HeaderTemplate>
            <asp:CheckBox CssClass="Checker" ID="cbSelectAll" runat="server" />
            </HeaderTemplate>
            <ItemTemplate>
            <asp:CheckBox ID="cbSelect" runat="server" />
            </ItemTemplate>
            </asp:TemplateColumn>
            </Columns>
  </asp:DataGrid>

Have the following jQuery that makes the header checkbox select all:

$(document).ready(function() {
$("#ctl00_ContentPlaceHolder1_dgMyGrid_ctl01_cbSelectAll").click(function() {
 $("#<%=dgID %> :checkbox").each(function(i)
 {
 this.checked = $("#ctl00_ContentPlaceHolder1_dgMyGrid_ctl01_cbSelectAll").is(":checked")
 });
 });
});

this works, but it's a bit ugly - I cant get the client ID for the header checkbox with
<%=cbSelectAll.ClientID%> (as I have done for the datagrid) Possibly because the javascript is rendered before that control. Is there a more elegant way for me to get the clientID of my checkbox out of the datagrid? I think it Would be better if I didnt hardcode the clientID like this.

Apologies if the answer to this is obvious, it's my first day trying jQuery! :)

2 Answers 2

1

This code to find your check box

 protected static Control FindControl(Control control, string controlId)
    {
        Control result;
        foreach (Control ctrl in control.Controls)
        {
            if (ctrl.ID == controlId)
            {
                result = ctrl;
                return result;
            }
            if (ctrl.Controls.Count != 0)
            {
                result = FindControl(ctrl, controlId);
                if (result != null)
                {
                    return result;
                }
            }
        }
        return null;
    }

And now you can use on aspx page:

<%= FindControl(dgMyGrid, "cbSelectAll").ClientID%>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, it's the solution I've ended up using. That said, I'd love to see a solution for this in jQuery Alone, but I'll mark this as the answer if there are no further responses.
1

Why not just reference the class of the checkbox 'cbSelectAll'.

<asp:CheckBox CssClass="Checker" ID="cbSelectAll" runat="server" />


$("#<%=dgID %> :checkbox").each(function(i)
{ 
    this.checked = $(".Checker").is(":checked");
});

If the class 'Checker' is used for multiple controls you can always assign a unique class.

<asp:CheckBox CssClass="Checker cbSelectAll" ID="cbSelectAll" runat="server" />

$("#<%=dgID %> :checkbox").each(function(i)
{ 
    this.checked = $(".cbSelectAll").is(":checked");
});

2 Comments

unfortunately this doesnt seem to work - the select all checkbox never gets becomes checked for some reason. Although if I check the other checkboxes, and click the checkall, they all become deselected, the opposite doesnt work (tested IE8 and FF).
Perhaps this is because it is inside the datagrid... as this is standard jQuery functionality I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.