0

我尝试在 GridView 中使用 DropDownList 设置“RoleName”,但出现此错误:

DataBinding: 'System.String' does not contain a property with the name 'RoleName'.

我做了你想象的任何事情,在不同的论坛上提到过。以下是代码:

 <asp:GridView ID="gvUsers" runat="server" PageSize="25" CssClass="gv" AutoGenerateColumns="False" DataKeyNames="UserName" DataSourceID="odsUsersInRoles" EnableModelValidation="True">
            <Columns>
    <asp:TemplateField HeaderText="Role">
        <EditItemTemplate>
            <asp:DropDownList ID="ddlRoles" runat="server" DataSourceID="odsRoles" DataTextField="RoleName" DataValueField="RoleName" SelectedValue='<%# Eval("RoleName") %>'></asp:DropDownList>
            <asp:ObjectDataSource ID="odsRoles" runat="server" SelectMethod="GetAllRolesButAdministrator" TypeName="cUsers"></asp:ObjectDataSource>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label3" runat="server" Text='<%# Bind("RoleName") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    ...
            </Columns>
        </asp:GridView>


public static string[] GetAllRolesButAdministrator()
{
    string[] roles = System.Web.Security.Roles.GetAllRoles();
    int removeIndex = Array.IndexOf(roles, "Administrator");

    if (removeIndex >= 0)
    {
        string[] newStrItems = new string[roles.Length - 1];

        for (int i = 0, j = 0; i < newStrItems.Length; i++, j++)
        {
            if (i == removeIndex)
                j++;
            newStrItems[i] = roles[j];
        }

        roles = newStrItems;
    }
    return roles;
}

请有人帮助我!!!

4

1 回答 1

0

好吧,我犯了一个非常愚蠢的错误:-(

我试图设置其 RoleName 的行根本没有 Role,因此它无法将“null”绑定到任何角色。

问题已解决:-)

于 2013-03-12T05:53:40.170 回答