I'm having a problem with a LoginView inside a GridView, and I want to programmatically set the roles for the LoginView's RoleGroups.
I started with a GridView with a LoginView in each row. For example, the gridview shows a list of products; if you are logged as an Admin, you also see a link to edit each product.
This works just fine, but I don't want to hardcode the name of the role, because I want to put this in a user control to be used with various sites, and the name of the role or roles might vary; I'd like to specify the name of this role in the web.config.
So I just removed the Roles="Admin" part from the RoleGroup, so that RoleGroup has no roles defined; then added this in my codebehind:
Private _Roles() As String = {ConfigurationManager.AppSettings("ProductEditorRole")}
For any LoginView that is NOT inside a GridView, I can just do this in the page load:
LoginView1.RoleGroups(0).Roles = _Roles
Works fine. For the LoginView inside the GridView, I did this in the GridView's RowDataBound event:
Dim lv As LoginView
lv = CType(e.Row.Cells(1).FindControl("LoginView1"), LoginView)
lv.RoleGroups(0).Roles = _Roles
My problem is that this only partly works. I do see the Edit Product link when I am logged in as an admin and not otherwise, so that part works. However, the Eval doesn't happen, so the links NavigateUrl doesn't get set. And if I try to do a FindControl
on lnkEdit, it can't be found, even though I check first to make sure I'm logged in as an Admin, and I can see the link displayed.
Is there a way around this? I really do not want to hardcode the name of the role!
The only way I could work around this is by adding a panel within the gridview like so: -
<asp:Panel ID="pnlAdmin" runat="server" visible="false">
<asp:LinkButton ID="btnDelete" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you wish to delete the store?');" runat="server"/>
</asp:Panel>
and then adding a bit of code to the item created event as follows: -
Protected Sub records_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles records.RowCreated
If Page.User.Identity.IsAuthenticated Then
Dim pnlAdmin As Panel = e.Row.FindControl("pnladmin")
pnlAdmin.visible = true
End If
End Sub
It seems that you need to do use the page.user.isauthenticated or page.user.isInRole(x) and then show / hide the panels as required.
This actually has a better scope of functionality than the loginView as you don't ahve to replicate panels for each role section and just show / hide what you want.
Celestine
Member
301 Points
82 Posts
Problem with LoginView/RoleGroups inside a GridView
Jun 18, 2006 06:38 PM|LINK
I started with a GridView with a LoginView in each row. For example, the gridview shows a list of products; if you are logged as an Admin, you also see a link to edit each product.
So something like this inside the GridView:
<ItemTemplate>
<p><%#Eval("ProductName")%></p>
<asp:LoginView ID=LoginView1 runat=server>
<RoleGroups>
<asp:RoleGroup Roles="Admin" >
<ContentTemplate>
<asp:HyperLink runat=server ID=lnkEdit
NavigateUrl='<%# String.Format("~/Admin/ProductEdit.aspx?ID={0}",
Eval("ProductID")) %>'
Text="Edit Product" />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
</ItemTemplate>
This works just fine, but I don't want to hardcode the name of the role, because I want to put this in a user control to be used with various sites, and the name of the role or roles might vary; I'd like to specify the name of this role in the web.config.
So I just removed the Roles="Admin" part from the RoleGroup, so that RoleGroup has no roles defined; then added this in my codebehind:
Private _Roles() As String = {ConfigurationManager.AppSettings("ProductEditorRole")}
For any LoginView that is NOT inside a GridView, I can just do this in the page load:
LoginView1.RoleGroups(0).Roles = _Roles
Works fine. For the LoginView inside the GridView, I did this in the GridView's RowDataBound event:
Dim lv As LoginView
lv = CType(e.Row.Cells(1).FindControl("LoginView1"), LoginView)
lv.RoleGroups(0).Roles = _Roles
My problem is that this only partly works. I do see the Edit Product link when I am logged in as an admin and not otherwise, so that part works. However, the Eval doesn't happen, so the links NavigateUrl doesn't get set. And if I try to do a FindControl on lnkEdit, it can't be found, even though I check first to make sure I'm logged in as an Admin, and I can see the link displayed.
Is there a way around this? I really do not want to hardcode the name of the role!
Delinquent
Member
393 Points
145 Posts
Re: Problem with LoginView/RoleGroups inside a GridView
Nov 22, 2007 10:28 AM|LINK
The only way I could work around this is by adding a panel within the gridview like so: -
<asp:Panel ID="pnlAdmin" runat="server" visible="false">
<asp:LinkButton ID="btnDelete" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are you sure you wish to delete the store?');" runat="server"/>
</asp:Panel>
and then adding a bit of code to the item created event as follows: -
Protected Sub records_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles records.RowCreated
If Page.User.Identity.IsAuthenticated Then
Dim pnlAdmin As Panel = e.Row.FindControl("pnladmin")
pnlAdmin.visible = true
End If
End Sub
It seems that you need to do use the page.user.isauthenticated or page.user.isInRole(x) and then show / hide the panels as required.
This actually has a better scope of functionality than the loginView as you don't ahve to replicate panels for each role section and just show / hide what you want.
Hope that helps :)