Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Check all Checkboxes in Gridview (.NET / C#)

  • 24-02-2009 11:15am
    #1
    Registered Users, Registered Users 2 Posts: 527 ✭✭✭


    Hi all,

    I have a gridview that has a column of checkboxes with a checkbox in the header template. I have it so that when the header checkbox is checked then all of the datarow's checkboxes are also checked.

    I have it working perfect in IE but it doesn't work in FF/Chrome.

    Javascript Code to Check-All:
    function SelectAllRows() {
        var chkAll = window.event.srcElement;
        var tbl = chkAll.parentNode.parentNode.parentNode.parentNode;
        for (var i = 1; i < tbl.rows.length; i++) {
            var chk = tbl.rows[i].cells[0].firstChild;
            chk.checked = chkAll.checked;
        }
    }
    

    Gridview's RowCreated Event:
    protected void MyGridview_RowCreated(object sender, GridViewRowEventArgs e)
    {
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    CheckBox chkAll = (CheckBox)e.Row.FindControl("chkSelectAll");
                    if (chkAll != null)
                    {
                        chkAll.Attributes.Add("onclick", "SelectAllRows()");
                    }
                }
    }
    

    Some of my Gridview code:
    <Columns>
                            <asp:TemplateField>
                                <HeaderStyle HorizontalAlign="Center" />
                                <HeaderTemplate>
                                    <asp:CheckBox ID="chkSelectAll" ToolTip="Click here to select/deselect all rows"
                                        runat="server" />
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkSelect" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
    


    Thanks in advance.


Comments

  • Closed Accounts Posts: 5,284 ✭✭✭pwd


    don't use javascript for this. use an eventhandler for when the checkbox is clicked in the header.

    Something like this whould work (syntax probably needs to be changed a bit)

    checkbox chk_select = new CheckBox();

    //code for eventhandler:
    foreach (Row r in MyGridview.Rows)
    {
    chk_select = (CheckBox)r.FindControl("select");
    chk_select.Value = 1;
    }


  • Registered Users, Registered Users 2 Posts: 527 ✭✭✭Sean^DCT4


    pwd wrote: »
    don't use javascript for this. use an eventhandler for when the checkbox is clicked in the header.

    Something like this whould work (syntax probably needs to be changed a bit)

    checkbox chk_select = new CheckBox();

    //code for eventhandler:
    foreach (Row r in MyGridview.Rows)
    {
    chk_select = (CheckBox)r.FindControl("select");
    chk_select.Value = 1;
    }

    I should have mentioned that I want this done on the Client and not the Server side.

    Anyway I just got it working now.

    GridView RowDataBound:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    ((CheckBox)e.Row.FindControl("cbSelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("cbSelectAll")).ClientID + "')");
                }
            }
    

    Javascript Function:
    function SelectAll(id) {
        var frm = document.forms[0];
        for (i = 0; i < frm.elements.length; i++) {
            if (frm.elements[i].type == "checkbox") {
                frm.elements[i].checked = document.getElementById(id).checked;
            }
        }
    }
    

    This code was tested and works in IE7, FF and Chrome.

    Thanks for the suggestion anyway.


  • Closed Accounts Posts: 5,284 ✭✭✭pwd


    Should have copped on that you wanted it to run on the client...most of what I'm doing at the moment is in Silverlight which runs on the client already. Glad you got it working


Advertisement