在ASP.Net的GridView中获取“勾选”复选框的rowID值

时间:2023-01-13 14:45:31

I am using checkboxs in a GridView. I need to determine the value in the cell in the 2nd column, if a checkbox has been checked.

我在GridView中使用checkboxs。如果选中了复选框,我需要确定第二列的单元格中的值。

I am using C# 2008 and ASP.net

我使用的是c# 2008和ASP.net。

                    <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="False" CellPadding="4" GridLines="None" Width="100%" AllowPaging="True" PageSize="20" 
                    onpageindexchanging="gvOrders_PageIndexChanging" ForeColor="#333333">
                    <Columns>
                        <asp:TemplateField HeaderText="VerifiedComplete" >
                            <ItemTemplate>
                                <asp:CheckBox ID="cbPOID" runat="server"/>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="PurchaseOrderID" HeaderText="PurchaseOrderID" HtmlEncode="False" ></asp:BoundField>
                        <asp:BoundField DataField="VENDOR_ID" HeaderText="Vendor ID"></asp:BoundField>
                        <asp:BoundField DataField="VENDOR_NAME" HeaderText="Vendor Name"></asp:BoundField>
                        <asp:BoundField DataField="ITEM_DESC" HeaderText="Item Desc"></asp:BoundField>
                        <asp:BoundField DataField="SYS_DATE" HeaderText="System Date"></asp:BoundField>
                    </Columns>
                    <FooterStyle CssClass="GridFooter" BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <PagerStyle CssClass="GridPager" ForeColor="#333333" BackColor="#FFCC66" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                    <HeaderStyle CssClass="GridHeader" BackColor="#990000" Font-Bold="True" ForeColor="White"  />
                    <RowStyle CssClass="GridItem" BackColor="#FFFBD6" ForeColor="#333333" />
                    <AlternatingRowStyle  CssClass="GridAltItem" BackColor="White" />
                </asp:GridView>





        protected void btnDisable_Click(object sender, EventArgs e)
    {
            foreach (GridViewRow gvr in gvOrders.Rows)
            {

                if (((CheckBox)gvr.FindControl("cbPOID")).Checked == true)
                {
                    string strPrimaryid = gvr.Cells[2].ToString();
                }
            }

    }

1 个解决方案

#1


2  

Another way to do this, to avoid accessing the cells of the GridView by index, is to convert the desired BoundField to a TemplateField in the designer. Then use the FindControl method to get the text value in that cell. By default, the designer will give the Label control of the new TemplateField a name like "Label1."

另一种方法是将所需的BoundField转换为设计器中的TemplateField,以避免通过索引访问GridView的单元。然后使用FindControl方法获取该单元格的文本值。默认情况下,设计器将为新TemplateField的标签控件提供一个名称,如“Label1”。

I would add some pictures to show the designer part, if that process were easier. At any rate, the aspx would change like so, which you could do manually too:

如果这个过程更简单的话,我会添加一些图片来显示设计部分。无论如何,aspx会像这样改变,你也可以手工做:

<asp:BoundField DataField="PurchaseOrderID" ...</asp:BoundField>

becomes

就变成了

<asp:TemplateField HeaderText="PurchaseOrderID">
 <ItemTemplate>
  <asp:Label ID="Label1" runat="server" Text='<%# Bind("PurchaseOrderID") %>'></asp:Label>
 </ItemTemplate>
 <EditItemTemplate>
  <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PurchaseOrderID") %>'></asp:TextBox>
 </EditItemTemplate>
</asp:TemplateField>

Then you would make this simple code change:

然后你要做一个简单的代码改变:

string strPrimaryid = gvr.FindControl("Label1").Text;

However, I think a better way to do this is by utilizing the DataKeys features of the GridView control. The first step would be to set the DataKeyNames property of your GridView:

但是,我认为更好的方法是利用GridView控件的DataKeys特性。第一步是设置GridView的DataKeyNames属性:

<asp:GridView ID="gvOrders" runat="server" DataKeyNames="PurchaseOrderID" ...>

Then, assuming your PurchaseOrderID column is of type int, you would change that same line to be:

然后,假设PurchaseOrderID列是int类型的,您将把相同的行更改为:

int primaryid = (int)gvOrders.DataKeys[gvr.RowIndex].Value;

#1


2  

Another way to do this, to avoid accessing the cells of the GridView by index, is to convert the desired BoundField to a TemplateField in the designer. Then use the FindControl method to get the text value in that cell. By default, the designer will give the Label control of the new TemplateField a name like "Label1."

另一种方法是将所需的BoundField转换为设计器中的TemplateField,以避免通过索引访问GridView的单元。然后使用FindControl方法获取该单元格的文本值。默认情况下,设计器将为新TemplateField的标签控件提供一个名称,如“Label1”。

I would add some pictures to show the designer part, if that process were easier. At any rate, the aspx would change like so, which you could do manually too:

如果这个过程更简单的话,我会添加一些图片来显示设计部分。无论如何,aspx会像这样改变,你也可以手工做:

<asp:BoundField DataField="PurchaseOrderID" ...</asp:BoundField>

becomes

就变成了

<asp:TemplateField HeaderText="PurchaseOrderID">
 <ItemTemplate>
  <asp:Label ID="Label1" runat="server" Text='<%# Bind("PurchaseOrderID") %>'></asp:Label>
 </ItemTemplate>
 <EditItemTemplate>
  <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("PurchaseOrderID") %>'></asp:TextBox>
 </EditItemTemplate>
</asp:TemplateField>

Then you would make this simple code change:

然后你要做一个简单的代码改变:

string strPrimaryid = gvr.FindControl("Label1").Text;

However, I think a better way to do this is by utilizing the DataKeys features of the GridView control. The first step would be to set the DataKeyNames property of your GridView:

但是,我认为更好的方法是利用GridView控件的DataKeys特性。第一步是设置GridView的DataKeyNames属性:

<asp:GridView ID="gvOrders" runat="server" DataKeyNames="PurchaseOrderID" ...>

Then, assuming your PurchaseOrderID column is of type int, you would change that same line to be:

然后,假设PurchaseOrderID列是int类型的,您将把相同的行更改为:

int primaryid = (int)gvOrders.DataKeys[gvr.RowIndex].Value;