0

我希望使用根据 SqlMembershipProvider 模式设置的数据库表创建所有用户的汇总表。到目前为止,我有:

<asp:GridView ID="UserAccounts" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="UserName" HeaderText="UserName" />
        <asp:BoundField DataField="Email" HeaderText="Email" />
        <asp:TemplateField HeaderText="Role">
            <ItemTemplate>
                <asp:TextBox ID="RoleText" ReadOnly="true" runat="server">
                </asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

对于表格和代码隐藏:

UserAccounts.DataSource = Membership.GetAllUsers();
UserAccounts.DataBind();

生成用户列表。但是,作为熟悉 SQLmembershipprovider 的人,与用户关联的角色位于多个单独的表中。因此,我不确定如何检索该信息并将其显示在每个用户旁边。

我目前正在考虑的解决方案是以某种方式逐行遍历表,然后在该用户名上调用 Roles.GetRolesForUser() 以检索信息,然后将其绑定到表。但是,我不确定如何执行此操作,以及它是否是最有效的方法。

4

2 回答 2

0

您可以使用此方法并使用 DataTable

public DataSet CustomGetAllUsers()    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt = ds.Tables.Add("Users");

        MembershipUserCollection muc;
        muc = Membership.GetAllUsers();

        dt.Columns.Add("UserName", Type.GetType("System.String"));
        dt.Columns.Add("Email", Type.GetType("System.String"));
        dt.Columns.Add("CreationDate", Type.GetType("System.DateTime"));

        /* Here is the list of columns returned of the Membership.GetAllUsers() method
         * UserName, Email, PasswordQuestion, Comment, IsApproved
         * IsLockedOut, LastLockoutDate, CreationDate, LastLoginDate
         * LastActivityDate, LastPasswordChangedDate, IsOnline, ProviderName
         */

        foreach(MembershipUser mu in muc) {
            DataRow dr;
            dr = dt.NewRow();
            dr["UserName"] = mu.UserName;
            dr["Email"] = mu.Email;
            dr["CreationDate"] = mu.CreationDate;
            dt.Rows.Add(dr);
        }
        return ds;
    }

绑定:

UserAccounts.DataSource = CustomGetAllUsers().Tables[0] ;
UserAccounts.DataBind();

你也可以调整你ItemTemplateEval.DataBinder

于 2012-09-20T17:24:29.047 回答
0

尝试绑定到

    var userRoles = from MembershipUser user in Membership.GetAllUsers()
                    from role in Roles.GetRolesForUser(user.UserName)
                    select new { 
                        UserName = user.UserName, Email = user.Email, Role = role };

或者

    var userRoles = from MembershipUser user in Membership.GetAllUsers()
                    let roles = Roles.GetRolesForUser(user.UserName)
                    select new {
                        UserName = user.UserName, 
                        Email = user.Email,
                        Roles = string.Join(", ",  roles) };
于 2012-09-20T17:38:49.553 回答