从Gridview |获取动态创建的复选框值Asp.net

时间:2022-12-07 22:07:36

I have this gridview where I need to populate more than 25 database tables. These table are lookup table and this gridview is for lookup table maintenance purpose.

我有这个gridview,我需要填充超过25个数据库表。这些表是查找表,此gridview用于查找表维护。

Instead of putting specific columns in gridview I directly bind the dataTable to gridview.

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

This below method assigns the (last column in gridview) active column's value which is "y"/"n" to checkbox.

下面的方法将(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>

code:

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

So the last column looks like this instead of y/n.

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

从Gridview |获取动态创建的复选框值Asp.net

Now I want to get the checkbox values, I am using this and thought it will work but it doesn't. Gives null value and "Object reference not set to an instance of an object".

现在我想得到复选框值,我正在使用它并认为它会起作用,但事实并非如此。给出null值并且“对象引用未设置为对象的实例”。

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

1 个解决方案

#1


0  

So what I did was I added a template field for checkbox and used this link's help to re arrange the columns so that the checkBox column be the last column.

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

And then I was able to get the checkbox value easily.

然后我就能轻松获得复选框值。

#1


0  

So what I did was I added a template field for checkbox and used this link's help to re arrange the columns so that the checkBox column be the last column.

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

And then I was able to get the checkbox value easily.

然后我就能轻松获得复选框值。