views:

23

answers:

2

Hi...

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 (with 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?

A: 

Hey,

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.

Brian
Sorry, should've been clearer about that - the dropdown is bound to the roles to get ALL the roles, the Bind call in selectedvalue, is to bind the role of the user being edited to the dropdown (so, if the user being edited is an "Admin" then the role "Admin" will be selected by default in the dropdown)
Dynde
A: 

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")) %>'
Dynde