如何从asp.net-membership中获取用户名和用户ID并将其绑定到下拉列表?

时间:2012-12-14 16:32:23

标签: c# asp.net asp.net-membership

我正在尝试从Membership.GetAllUsers()获取userName和userID以将其绑定到下拉列表

   private void Bind_usersList()
    {
        var dictionary = Membership.GetAllUsers().Cast<MembershipUser>();
    }

1 个答案:

答案 0 :(得分:4)

MembershipUser没有“userID”,但ProviderUserKey可能是Guid(f {。在SqlMembershipProvider中)。

这应该有效:

MembershipUserCollection members = Membership.GetAllUsers();
DdlUsers.DataSource = members.Cast<MembershipUser>()
    .ToDictionary(m => m.ProviderUserKey, m =>  m.UserName);
DdlUsers.DataValueField = "Key";
DdlUsers.DataTextField = "Value";
DdlUsers.DataBind();