1

这是引发异常的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;
        }
    }
4

1 回答 1

3

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

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

(对不起,这是从内存中获得的,但这应该在您的 ItemDataBound 方法中)

于 2009-06-21T01:07:45.410 回答