Click here to Skip to main content
16,012,468 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following GridView:
ASP.NET
<td id="TableGrid" align="left">
                    <asp:Panel ID="pnlDanhSach" Width="100%" runat="server">
                        <asp:GridView ID="grvDanhSach" OnPageIndexChanging="grvDanhSach_PageIndexChanging" AutoGenerateColumns="False" SkinID="SkinDefault" BorderStyle="None" BorderWidth="1px"
                            Width="100%" runat="server">
                            <Columns>
                                <asp:TemplateField>
                                    <HeaderTemplate>
                                        <asp:CheckBox ID="chkHeader" runat="server" />
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:CheckBox ID="chkRow" runat="server" />
                                    </ItemTemplate>
                                    <HeaderStyle Width="40px" HorizontalAlign="Center" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderStyle-HorizontalAlign="Center" HeaderText="UserName" 
                                    SortExpression="UserName" Visible="False">
                                    <ItemTemplate>
                                        <asp:Label ID="lblUserName" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="HoVaTen" HeaderText="Họ và tên" 
                                    SortExpression="HoVaTen" />
                                <asp:BoundField DataField="RoleName" HeaderText="Quyền hạn" 
                                    SortExpression="RoleName" />
                                <asp:BoundField DataField="TenPhong" HeaderText="Phòng ban" 
                                    SortExpression="TenPhong" />
                            </Columns>
                        </asp:GridView>
                    </asp:Panel>
                </td>

I use jQuery to check / uncheckall
JavaScript
<script type="text/javascript">
        $("[id*=chkHeader]").live("click", function () {
            var chkHeader = $(this);
            var grid = $(this).closest("table");
            $("input[type=checkbox]", grid).each(function () {
                if (chkHeader.is(":checked")) {
                    $(this).attr("checked", "checked");
                    $("td", $(this).closest("tr")).addClass("selected");
                } else {
                    $(this).removeAttr("checked");
                    $("td", $(this).closest("tr")).removeClass("selected");
                }
            });
        });
        $("[id*=chkRow]").live("click", function () {
            var grid = $(this).closest("table");
            var chkHeader = $("[id*=chkHeader]", grid);
            if (!$(this).is(":checked")) {
                $("td", $(this).closest("tr")).removeClass("selected");
                chkHeader.removeAttr("checked");
            } else {
                $("td", $(this).closest("tr")).addClass("selected");
                if ($("[id*=chkRow]", grid).length == $("[id*=chkRow]:checked", grid).length) {
                    chkHeader.attr("checked", "checked");
                }
            }
        });
</script>

In behide code I get the following:
C#
private string GetUserDoc()
    {
        int intNumCheck = 0;
        string strUserDoc = "";
        if (grvDanhSach.Rows.Count > 0)
        {
            for (int i = 0; i < grvDanhSach.Rows.Count; i++)
            {
                CheckBox chkChon = (CheckBox)grvDanhSach.Rows[i].FindControl("chkRow");
                Label lblUserName = (Label)grvDanhSach.Rows[i].FindControl("lblUserName");
                if (chkChon.Checked == true)
                {
                    strUserDoc = lblUserName.Text + "|";
                    intNumCheck++; 
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Bạn cần chọn ít nhất một cán bộ');", true);
                }
                if (intNumCheck > 1)
                {
                    strUserDoc += lblUserName.Text + "|";
                }
            }
        }
        return strUserDoc;
    }

but when I check the checkbox, then it does not understand and jump to the else statement. Do not know when to use jQuery it understands their value checks are not right. Who knows just me with. Thank you.
Posted
Updated 16-Aug-15 17:06pm
v2

I have used the code present here Selecting / Deselecting all the CheckBoxes Inside a GridView[^]. This works perfectly.

I think the code you are using is doing something wrong. You can debug or use the code described in above link.
 
Share this answer
 
Thank Tadit Dash.This works perfectly
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900