Getting at the Membership Roles.GetAllRoles()
For the WebGallery2 project, I’ve moved away from my own user management and opted for the .NET Roles and Membership “features”. I do like the functionality (built in is good sometimes), and the ease of maintenance; however, the management of roles and memberships SUCKS—especially when you want to use that information in your application.
One such is our Galleries. Each gallery has an associated Role that is checked by the User.IsInRole() method.
However, when editing a gallery, I want to see the enumeration for all roles—so I can pick and choose in a drop down list. Unfortunately, there’s no really good way to get at the Roles list and drop it into a GridView’s FooterRow—at least not that seems to work without 40–50 lines of code.
So, I ended up cheating a bit.
Inside our LINQ DataContext, I added a non-LINQ-releated method called “GetAllRoles()” that returns a string array (string). To me, this is still data, data retrieval, and fits into the DataContext partial class.
public string GetAllRoles()
I can now call that method like:
WebGalleryDataContext db = new WebGalleryDataContext();
string rolesList = db.GetAllRoles();
That’s great. However, if I wanted to use a LinqDataSource adapter, that won’t work—it doesn’t exist inside a LINQ Table context. To fix that, the good old ObjectDataAdapter comes to the rescue.
<asp:ObjectDataSource ID=”Roles” runat=”server”
Then, in my EditItemTemplate (of a GridView), I can populate the DropDownList with the roles in the system:
<asp:DropDownList runat=”server” ID=”EditRole”
SelectedValue=’<%# Bind(“Role”) %>‘ />
If there’s a better way, I’d LOVE to hear it.🙂
Update: Whoops, http://localhost for the photo probably isn’t gonna work.