e.Item.FindControl 抛出未设置对象实例的对象引用
这是引发异常的 Repeater 代码部分...“Computer.Administrators”是 StringCollection 对象。 调试器显示“AdminsEnumerator.Current”获取正确的字符串值,但当“txtAdministrators.Text”尝试设置值时 - 引发异常。 请帮忙出出主意。
<asp:Repeater ID="repeatAdministrators" OnItemDataBound="repeatAdministrators_ItemDataBound" runat="server">
<HeaderTemplate>
<tr>
<td class="formLabel">
Administrators:
</td>
<td class="formInputText">
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtAdministrators" runat="server" MaxLength="50" Enabled="False"></asp:TextBox><br />
</ItemTemplate>
<FooterTemplate>
</td> </tr>
<tr>
</FooterTemplate>
</asp:Repeater>
这是后面的代码。
protected void btnPing_Click(object sender, EventArgs e)
{
//...
repeatAdministrators.DataSource = Computer.Administrators;
repeatAdministrators.DataBind();
}
protected void repeatAdministrators_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
TextBox txtAdministrators = e.Item.FindControl("txtAdministrators") as TextBox;
StringEnumerator AdminsEnumerator = Computer.Administrators.GetEnumerator();
while (AdminsEnumerator.MoveNext())
{
txtAdministrators.Text = AdminsEnumerator.Current;
}
}
Here is the part of Repeater code that throws exception... "Computer.Administrators" is StringCollection object. Debugger shows that "AdminsEnumerator.Current" gets correct string value but when "txtAdministrators.Text" tries to set value - exception is thrown. Please help with ideas.
<asp:Repeater ID="repeatAdministrators" OnItemDataBound="repeatAdministrators_ItemDataBound" runat="server">
<HeaderTemplate>
<tr>
<td class="formLabel">
Administrators:
</td>
<td class="formInputText">
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtAdministrators" runat="server" MaxLength="50" Enabled="False"></asp:TextBox><br />
</ItemTemplate>
<FooterTemplate>
</td> </tr>
<tr>
</FooterTemplate>
</asp:Repeater>
And here is code behind.
protected void btnPing_Click(object sender, EventArgs e)
{
//...
repeatAdministrators.DataSource = Computer.Administrators;
repeatAdministrators.DataBind();
}
protected void repeatAdministrators_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
TextBox txtAdministrators = e.Item.FindControl("txtAdministrators") as TextBox;
StringEnumerator AdminsEnumerator = Computer.Administrators.GetEnumerator();
while (AdminsEnumerator.MoveNext())
{
txtAdministrators.Text = AdminsEnumerator.Current;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要确保您不在标题项中:(
抱歉,这是从记忆中得出的,但这应该放在您的 ItemDataBound 方法中)
You need to make sure you're not in a header item:
(sorry, this is from memory, but that should go in your ItemDataBound method)