我希望根据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>
表和codebehind:
UserAccounts.DataSource = Membership.GetAllUsers();
UserAccounts.DataBind();
生成用户列表。但是,作为熟悉SQLmembershipprovider的人,与用户关联的角色位于多个单独的表中。因此,我不确定如何检索该信息并将其显示在每个用户旁边。
我目前正在考虑的解决方案是以某种方式逐行遍历表,然后在该用户名上调用Roles.GetRolesForUser()以检索信息,然后将其绑定到表。但是,我不确定如何做到这一点,如果它甚至是最有效的方法。
答案 0 :(得分: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();
您还可以使用ItemTemplate
Eval.DataBinder
答案 1 :(得分: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) };