C#-FormView.FindControl():对象引用错误
作者:互联网
我有一个formview,其中在tr / td的内部有几个文本框.我正在尝试使用.FindControl方法获取文本框,但它又返回了null. FormView始终处于“编辑”模式(因此,我始终处于EditItemTemplate中),并且我试图将querystring值加载到上一页的文本框中,因此我确实需要在page_load上进行此操作.我一直在Gridviews上执行以下操作:
txtFirstName = (TextBox)fvGeneralInfo.FindControl("txtFirstName");
或像这样:
txtFirstName = (TextBox)fvGeneralInfo.FooterRow.FindControl("txtFirstName");
或像这样:
txtFirstName = (TextBox)fvGeneralInfo.Rows.FindControl("txtFirstName");
是什么赋予了?
<asp:FormView ID="fvGeneralInfo" runat="server"
DataSourceID="objInstructorDetails"
OnItemCommand="fvGeneralInfo_ItemCommand"
OnItemUpdated="fvGeneralInfo_ItemUpdated"
DefaultMode="Edit"
DataKeyNames="InstructorID" >
<EditItemTemplate>
<table>
<tr>
<td colspan="2" class="Admin-SubHeading" style="padding-left:10px;">General Info:</td>
</tr>
<tr>
<td class="Admin-FieldLabel">ID:</td>
<td><asp:TextBox ID="txtInstructorId" runat="server" CssClass="Admin-Textbox" ReadOnly="true" Text='<%# Bind("InstructorID") %>' /></td>
</tr>
<tr>
<td class="Admin-FieldLabel">First Name:</td>
<td><asp:Textbox ID="txtFirstName" runat="server" CssClass="Admin-Textbox" Text='<%# Bind("FirstName") %>' /></td>
</tr>
</table>
</EditItemTemplate>
</asp:FormView>
解决方法:
abatishchev的答案是正确的,尽管我发现这种变化更为简洁:它避免了必须显式调用DataBind()的情况.
<asp:FormView ID="fvMember" runat="server" DataSourceID="tblMembers" DefaultMode="Insert" OnDataBound="DataBound">...</asp:FormView>
protected void DataBound(object sender, EventArgs e)
{
if (fvMember.CurrentMode == FormViewMode.Edit)
{
Label lblSubmit = fvMember.FindControl("lblSubmit") as Label;
...
}
}
标签:findcontrol,formview,asp-net,c,net 来源: https://codeday.me/bug/20191024/1920138.html