[英]FindControl Returns a Null Reference
I have two textboxes and I use FindControl()
to access them: 我有两个文本框,并且使用
FindControl()
来访问它们:
<tr>
<td align="right">
<asp:Label ID="LastNameLabel" AssociatedControlID="LastName" runat="server" /></td>
<td>
<asp:TextBox ID="LastName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="LastNameRequired" runat="server" ControlToValidate="LastName" Display="Dynamic"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td align="right">
<asp:Label ID="PrimaryCompanyLabel" AssociatedControlID="PrimaryCompany" runat="server" /></td>
<td>
<asp:TextBox ID="PrimaryCompany" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="PrimaryCompanyRequired" runat="server" ControlToValidate="PrimaryCompany" Display="Dynamic"></asp:RequiredFieldValidator></td>
</tr>
The Textbox LastName
is being accessed fine but the second, PrimaryCompany
is returning a null reference 可以很好地访问文本框的
LastName
但第二个PrimaryCompany
返回空引用
They are being access by: 正在通过以下方式访问它们:
private IEditableTextControl _txtLastName;
protected IEditableTextControl txtLastName
{
get
{
if (_txtLastName == null)
{
_txtLastName = (IEditableTextControl)this.CreateUserStep.ContentTemplateContainer.FindControl("LastName");
}
return _txtLastName;
}
}
private IEditableTextControl _txtPrimaryCompany;
protected IEditableTextControl txtPrimaryCompany
{
get
{
if (_txtPrimaryCompany == null)
{
_txtPrimaryCompany = (IEditableTextControl)this.CompleteStep.ContentTemplateContainer.FindControl("PrimaryCompany");
}
return _txtPrimaryCompany;
}
}
This code is from the SharePoint2013 FBAPack in CodePlex. 此代码来自CodePlex中的SharePoint2013 FBAPack。 The
LastName
field is built-in while the PrimaryCompany
field is being added by me. 我正在添加
PrimaryCompany
字段时, LastName
字段是内置的。
I'm only showing this part of the code as the null reference
is being thrown here. 我只显示这部分代码,因为此处抛出了
null reference
。 Am I missing something? 我想念什么吗?
Are you sure it's here that the null reference is being thrown? 您确定是在这里引发了空引用吗? Do you have a line number?
你有电话号码吗? Here the control is actually being checked for null.
这里实际上是检查控件是否为空。
My guess is that it's actually elsewhere in the code that it happens, because the .Text property is null. 我的猜测是它实际上在代码中的其他地方发生,因为.Text属性为null。 For example if you do something like:
例如,如果您执行以下操作:
txtPrimaryCompany.Text.Trim() txtPrimaryCompany.Text.Trim()
For some reason IEditableTextControl
doesn't work on the field that I created. 由于某些原因,
IEditableTextControl
在创建的字段上不起作用。 I used TextBox
instead and it worked. 我改用
TextBox
,它起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.