views:

214

answers:

1

Here is the snapshot of repeater code. I have problems with geting all the values. In result my "repeater" prints only first element in collection.

        <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 the code behind where I want to change read index in collection. AdminsEnumerator.MoveNext(); is not working as I expected. Adding something like int t; i++; StringCollection[i] is annoying. Are there easier solutions?

    protected void repeatAdministrators_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            TextBox txtAdministrators = e.Item.FindControl("txtAdministrators") as TextBox;
            StringEnumerator AdminsEnumerator = Computer.Administrators.GetEnumerator();

            AdminsEnumerator.MoveNext();
            txtAdministrators.Text = AdminsEnumerator.Current;
        }
    }
+2  A: 

Your code wouldn't work because you'll be getting a new enumerator for each item that is databound and starting over at the beginning. Try this instead:

Repeater ItemTemplate:

<ItemTemplate>
    <asp:TextBox ID="txtAdministrators" runat="server" Text="<%#Container.DataItem %>"></asp:TextBox>
</ItemTemplate>

Code (Page_Load):

StringCollection administrators = new StringCollection();
administrators.AddRange(new string[] { "Bob", "Bill", "Frank" });
repeatAdministrators.DataSource = administrators;            
repeatAdministrators.DataBind();
Richard Hein
Thanks a lot!!! It really easy :)
Juri Bogdanov