Bind a users role to dropdown?
I'm trying to bind a selected user's role to a dropdown-list. The purpose of this is to be able to change said user's role.
I'm attempting this inside a formview hooked up to a linqdatasource which contains a row from the aspnet_User table
.
The dropdown list is hooked up to a linqdatasource of all the roles in the aspnet_Roles table (wit开发者_StackOverflow社区h DataValueField="RoleID", DataTextField="RoleName")
.
I figured it would be possible with something like:
SelectedValue='<%# Bind("aspnet_UsersInRole[0].aspnet_Role.RoleID") %>'
But this throws a parser exception, about the bind call not being correctly formatted.
The roles are there, they show up when I remove the SelectedValue
Can anyone point me in the right direction?
If you are binding aspnet_roles table to the drop down, then why does the code sample use aspnet_usersinrole? Instead, if you want to bind to aspnet_role object, do:
SelectedValue='<%# Bind("RoleID") %>'
Instead; that will hookup to the right property. If you want to bind aspnet_usersinrole, if possible in the LINQDataSource, from the resuling aspnet_usersinrole, do a select operation to select the aspnet_role object instead for this.
Solved - I ended up just making a method in the codebehind which returned the roleID, like so:
public Guid GetRoleID(Guid userID) {
DBDataContext db = new DBDataContext();
aspnet_User user = db.aspnet_Users.SingleOrDefault(o => o.UserId == userID);
return user.aspnet_UsersInRoles[0].aspnet_Role.RoleId;
}
and then in the markup do:
SelectedValue='<%# GetRoleID((Guid)Eval("UserID")) %>'
精彩评论