ASP.NET C#(3.5),Membership,如何使用GridView中的DropDownList设置RoleName?

时间:2013-03-12 05:31:18

标签: gridview drop-down-menu membership

我尝试在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;
}

请有人帮助我!!!

1 个答案:

答案 0 :(得分:0)

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

我试图设置其RoleName的行,根本没有角色,所以它无法将'null'绑定到任何角色。

问题得到解决: - )