0

这是中继器代码的快照。我在获取所有值时遇到问题。结果我的“中继器”只打印集合中的第一个元素。

        <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>

这里是我想更改集合中读取索引的代码。AdminsEnumerator.MoveNext();没有按我的预期工作。添加类似的东西int t; i++; StringCollection[i]很烦人。有更简单的解决方案吗?

    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;
        }
    }
4

1 回答 1

2

您的代码将无法工作,因为您将为每个数据绑定的项目获取一个新的枚举器,并从头开始。试试这个:

中继器项目模板:

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

代码(Page_Load):

StringCollection administrators = new StringCollection();
administrators.AddRange(new string[] { "Bob", "Bill", "Frank" });
repeatAdministrators.DataSource = administrators;            
repeatAdministrators.DataBind();
于 2009-06-21T02:24:07.190 回答