简体   繁体   English

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

[英]e.Item.FindControl throws Object reference not set to an instance of an object

Here is the part of Repeater code that throws exception... "Computer.Administrators" is StringCollection object. 这是引发异常的Repeater代码的一部分...“ Computer.Administrators”是StringCollection对象。 Debugger shows that "AdminsEnumerator.Current" gets correct string value but when "txtAdministrators.Text" tries to set value - exception is thrown. 调试器显示“ AdminsEnumerator.Current”获得正确的字符串值,但是当“ txtAdministrators.Text”尝试设置值时-引发异常。 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;
        }
    }

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) (对不起,这是从内存中获取的,但是应该放在您的ItemDataBound方法中)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 grid.FindControl 期间出现错误“对象引用未设置为对象的实例” - Error 'Object reference not set to an instance of an object' during grid.FindControl 使用FindControl时,对象引用未设置为对象的实例 - Object reference not set to an instance of an object when using FindControl GridView-&gt;从SQL进行数据绑定。 FindControl对象引用未设置为RowDataBound中的实例 - GridView -> Databinding from SQL. FindControl object reference not set to an instance in RowDataBound Silverlight添加到列表 <T> 抛出未设置为对象实例的对象引用 - Silverlight adding to List<T> throws Object reference not set to an instance of an object Roles.GetRolesForUser抛出异常未将对象引用设置为对象的实例 - Roles.GetRolesForUser throws exception Object reference not set to an instance of an object .dbml .cs文件引发对象引用未设置为对象的实例 - .dbml .cs File throws Object reference not set to an instance of an object 变量具有值,但抛出未设置为对象实例的对象引用 - A variable has a value but throws an object reference not set to an instance of an object PdfContentByte.SetFontAndSize()引发“对象引用未设置为对象实例” - PdfContentByte.SetFontAndSize() throws “Object reference not set to instance of object” ManualResetEvent引发NullReferenceException:对象引用未设置为对象的实例 - ManualResetEvent throws NullReferenceException: Object reference not set to an instance of an object Telerik Radgrid rebind()抛出未将对象引用设置为对象错误的实例 - Telerik radgrid rebind() throws Object reference not set to instance of object error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM