e.Item.FindControl 抛出未设置对象实例的对象引用

发布于 2024-07-25 00:26:06 字数 1484 浏览 6 评论 0原文

这是引发异常的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰ツ都不明白 2024-08-01 00:26:06

您需要确保您不在标题项中:(

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
}

抱歉,这是从记忆中得出的,但这应该放在您的 ItemDataBound 方法中)

You need to make sure you're not in a header item:

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
}

(sorry, this is from memory, but that should go in your ItemDataBound method)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文