FindControl如何在GridView中工作?

时间:2010-07-12 19:39:12

标签: c# asp.net gridview findcontrol commandfield

我正在尝试使用GridView编辑更新数据库,更新CommandField。我有两个可编辑的字段,在编辑模式下显示为文本框。单击提交时,我试图将文本框值放入变量中以供使用,但我无法访问它们。两列名称是“EOR”和“CategoryName”。我在其他论坛上发现了一些尝试类似的建议:

protected void ResultGridView_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    TextBox txtEor = (TextBox)myGridName.Rows[e.RowIndex].FindControl("EOR"); 

当我调试程序时,txtEor始终为null。我唯一能想到的是我没有正确引用细胞。我将Headertext,AccessibleHeaderText,DataField和SortExpression设置为“EOR”,但它仍然只是为空。

非常感谢任何帮助!

asp for gridview:

<asp:GridView ID="grdEOR" runat="server" BackColor="White"
            BorderColor="#999999" OnPageIndexChanging="grdEor_PageIndexChanging"
            BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical"
            AllowPaging="True"
            PageSize="15" AutoGenerateColumns="False" onrowediting="grdEOR_RowEditing" 
                        onrowcancelingedit="grdEOR_RowCancelingEdit" 
                        onrowupdating="grdEOR_RowUpdating" onrowdeleting="grdEOR_RowDeleting" 
                        ShowFooter="True">
            <PagerSettings Mode="NumericFirstLast" />
            <Columns>
                <asp:BoundField DataField="EORCategoryID" HeaderText="EORCategoryID" 
                    SortExpression="EORCategoryID" ReadOnly="True">
                </asp:BoundField>
                <asp:BoundField DataField="EOR" HeaderText="EOR" SortExpression="EOR" 
                    AccessibleHeaderText="EOR"/>
                <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" 
                    SortExpression="CategoryName" />
                <asp:CommandField ButtonType="Button" ShowDeleteButton="True" 
                    ShowEditButton="True" />


            </Columns>
            <FooterStyle BackColor="#CCCCCC" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="#CCCCCC" BorderColor="Black" 
                BorderStyle="Solid" BorderWidth="5px" />
        </asp:GridView>

1 个答案:

答案 0 :(得分:1)

我终于找到了一种有效的方法:

        string newEor = ((TextBox)grdEOR.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string newCategoryName = ((TextBox)grdEOR.Rows[e.RowIndex].Cells[2].Controls[0]).Text;