【问题标题】:Get dynamically created checkbox value from Gridview | Asp.net从 Gridview 获取动态创建的复选框值 | ASP.NET
【发布时间】:2017-10-02 05:52:28
【问题描述】:

我有这个 gridview,我需要在其中填充超过 25 个数据库表。这些表是查找表,此网格视图用于查找表维护。

我没有将特定列放在 gridview 中,而是直接将 dataTable 绑定到 gridview。

以下方法将(gridview 中的最后一列)活动列的值“y”/“n”分配给复选框。

<asp:GridView ID="gvTables" runat="server" CssClass="Grid" Width="100%" AutoGenerateColumns="true" Font-Names="Arial"
                                    BorderWidth="1" PagerSettings-Mode="Numeric" Font-Size="8.7pt" Font-Bold="false" AlternatingRowStyle-BackColor="#f4f4f4"
                                    HeaderStyle-BackColor="LightGray" AllowPaging="True" ShowFooter="false" PageSize="12"
                                    OnPageIndexChanging="gvTables_PageIndexChanging"
                                    OnRowDataBound="gvTables_RowDataBound"
                                    OnRowCreated="gvTables_RowCreated"
                                    OnSelectedIndexChanged="gvTables_SelectedIndexChanged">
                                    <PagerStyle CssClass="pgr" />
                                </asp:GridView>

代码:

private void SetCheckBoxesInGV()
    {
        ////add checkboxes in place of active
        for (int i = 0; i < gvTables.Rows.Count; i++)
        {
            System.Web.UI.WebControls.CheckBox chk = new System.Web.UI.WebControls.CheckBox();
            chk.ID = "chk";
            chk.Checked = gvTables.Rows[i].Cells[gvTables.Rows[0].Cells.Count - 1].Text.Trim() == "y" ? true : false;
            gvTables.Rows[i].Cells[gvTables.Rows[0].Cells.Count - 1].Controls.Add(chk);
            gvTables.Rows[i].Cells[gvTables.Rows[0].Cells.Count - 1].ControlStyle.CssClass = "ItemCenter";
        }
    }

所以最后一列看起来像这样,而不是 y/n。

现在我想获取复选框值,我正在使用它并认为它可以工作,但它没有。给出空值和“对象引用未设置为对象的实例”。

((CheckBox)gvTables.Rows[i].Cells[gvTables.Rows[0].Cells.Count - 1].FindControl("chk")).Checked

【问题讨论】:

    标签: c# asp.net gridview checkbox


    【解决方案1】:

    所以我所做的是为复选框添加了一个模板字段,并使用此link's 帮助重新排列列,以便复选框列成为最后一列。

    然后我就可以轻松获得复选框的值了。

    【讨论】:

      猜你喜欢
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-07
      相关资源
      最近更新 更多