单击GridView上的复选框时获取值 [英] Get the value when clicking the checkbox on the GridView

查看:69
本文介绍了单击GridView上的复选框时获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下GridView:

I have the following GridView:

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



我使用jQuery检查/ uncheckall


I use jQuery to check / uncheckall

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

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;
    }



但是当我选中复选框时,它不理解并跳转到else语句。不知道什么时候使用jQuery它理解他们的价值检查是不对的。谁知道我。谢谢。


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.

推荐答案

[id * = chkHeader])。live( 点击 function (){
var chkHeader =
("[id*=chkHeader]").live("click", function () { var chkHeader =


this );
var grid =
(this); var grid =


this )。nearest( table);
(this).closest("table");


这篇关于单击GridView上的复选框时获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆