始终在网格中将复选框设置为false

Ric*_*cha 3 asp.net checkbox gridview

我在网格中有复选框.我试图从代码隐藏访问它们并获取已检查/未检查行的数据.但是即使在检查了复选框后,我将它们作为Checked属性仅作为false:

ASPX:

  <table width="100%">
            <asp:GridView ID="grdRequestsPending" runat="server" Width="100%" AutoGenerateColumns="false"
                BorderWidth="1px" BorderStyle="Solid" Style="margin-left: 0px" BorderColor="#ffcc00"
                RowStyle-BorderColor="#ffcc00" RowStyle-BorderStyle="Solid" RowStyle-BorderWidth="1px"
                GridLines="Both" DataKeyNames="ReqID,ApproverComments" On="grdRequestsPending_ItemDataBound" OnRowDataBound="grdRequestsPending_RowDataBound"
                OnPreRender="grdRequestsPending_PreRender">
                <RowStyle CssClass="dbGrid_Table_row" />
                <HeaderStyle CssClass="dbGrid_Table_Header" />
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <asp:Label ID="lblSelect" Text="Select All" runat="server"></asp:Label><br />
                            <asp:CheckBox ID="SelectAll" onclick="javascript:checkAllBoxes(this);" TextAlign="Left"
                                runat="server" />
                        </HeaderTemplate>
                        <ItemStyle Width="2%" />
                        <ItemTemplate>
                            <asp:CheckBox ID="chkReq" runat="server"/>
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Center" Width="7%" />
                    </asp:TemplateField>
       </Columns>
Run Code Online (Sandbox Code Playgroud)

但是当我检查这些时,我总是将它们视为代码背后的错误:

        protected void UpdateVMRequestStatusByCapSupLead(int StatusId)
    {
        try
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("ReqId", typeof(int));
            dt.Columns.Add("StatusId", typeof(int));
            dt.Columns.Add("ModifiedBy", typeof(string));
            dt.Columns.Add("ModifiedDate", typeof(string));
            dt.Columns.Add("txtCommentSupLead", typeof(string));
            foreach (GridViewRow gr in grdRequestsPending.Rows)

            {
                CheckBox chk = (CheckBox)gr.FindControl("chkReq");

                if (chk.Checked)
                {
                    strReqId = strReqId + grdRequestsPending.DataKeys[gr.RowIndex].Value.ToString() + ',';

                    TextBox txtCommentSupLead = (TextBox)gr.FindControl("txtCommentSupLead");
                    dt.Rows.Add(dt.NewRow());
                    dt.Rows[dt.Rows.Count - 1]["ReqId"] = Convert.ToInt32(grdRequestsPending.DataKeys[gr.RowIndex].Value);
                    dt.Rows[dt.Rows.Count - 1]["StatusId"] = StatusId;
                    dt.Rows[dt.Rows.Count - 1]["ModifiedBy"] = Session["UserAccentureID"].ToString();
                    dt.Rows[dt.Rows.Count - 1]["txtCommentSupLead"] = txtCommentSupLead.Text;
                    dt.Rows[dt.Rows.Count - 1].AcceptChanges();
                    dt.Rows[dt.Rows.Count - 1].SetModified();
                }
            }
Run Code Online (Sandbox Code Playgroud)

我没有得到问题.我也正确地获得了控制权.

Tim*_*ter 9

我假设你总是数据绑定你的GridView而不仅仅是if(!Page.IsPostBack)....

所以把它放进去 page_load

protected void Page_Load(object sender, EventArgs e)
{                        
    if(!Page.IsPostBack)
    {
        DataBindControls(); // like GridView etc.
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你DataBind控制他们将失去他们的变化和ViewState.甚至事件也不会被触发.所以你应该只在第一次加载时才这样做EnableViewState="true".