1

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

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

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

任何帮助将不胜感激!

用于gridview的asp:

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

1 回答 1

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;
于 2010-07-12T20:57:58.917 回答