views:

961

answers:

1

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;
        }
    }
+2  A: 

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)

Jonas
I confirm. (Fixed the syntax for you)
GoodEnough
Ok, thnks :)It really works
Juri Bogdanov
Thanks, on my mac, didn't have quick access to MSDN or Intellisense :)
Jonas