3

我在网格中有复选框。我正在尝试从代码隐藏中访问它们并获取选中/未选中行的数据。但即使在选中复选框后,我也将它们作为 Checked 属性获取为 false:

ASP:

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

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

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

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

4

2 回答 2

9

我假设您总是对 GridView 进行数据绑定,而不仅仅是if(!Page.IsPostBack)....

所以把这个放进去page_load

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

如果您DataBind控制它们,它们将丢失更改和 ViewState。甚至事件都不会被触发。所以你应该只在第一次加载 if 时这样做EnableViewState="true"

于 2012-11-19T15:37:29.980 回答
0

这是一个在gridview中填充复选框并在单击按钮时获取值的简单示例

默认.aspx

<asp:GridView ID="gv" 
              runat="server" 
              AutoGenerateColumns="false" 
              OnRowDataBound="gv_RowDataBound">
  <Columns>
    <asp:TemplateField>
      <ItemTemplate>
        <asp:CheckBox ID="chkReq" runat="server" />
      </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>
<br />
<asp:Button ID="btnSubmit" 
            runat="server" 
            OnClick="btnSubmit_Click" 
            Text="Submit" />

默认.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    //Create 20 rows
    gv.DataSource = Enumerable.Range(1, 20);
    gv.DataBind();
  }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
  var isCheckedList = new List<bool>();

  for(var index = 0; index < gv.Rows.Count; ++index)
  {
    var chkReq = (CheckBox)gv.Rows[index].FindControl("chkReq");
    isCheckedList.Add(chkReq.Checked);
  }

  //Look at isCheckedList to get a list of current values.
  System.Diagnostics.Debugger.Break();
}

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    var index = e.Row.RowIndex;

    //Strongly Bind Controls
    var chkReq = (CheckBox)e.Row.FindControl("chkReq");
    chkReq.Text = "Item " + index.ToString();
  }
}

由于您没有显示所有代码,因此可能是 Tim Schmelter 所说的问题,也可能是其他问题。此示例为您提供了从网格视图中检索复选框值所需的基础知识。

于 2012-11-19T15:51:04.907 回答