FindControl如何在GridView中工作?
-
14-09-2020 - |
题
我正在尝试使用gridview编辑更新数据库,Update 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”,但它仍然只是unull。
任何帮助将非常感谢!
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>
. 解决方案
我终于找到了一种方法:
string newEor = ((TextBox)grdEOR.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string newCategoryName = ((TextBox)grdEOR.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
. 不隶属于 StackOverflow