[RESOLVED] Disable a Image button or Hyperlink button based on ASP.Net Role

Ok, here's my issue I have four Imagebuttons that I want to disable depending on the Role the user belongs too.  I know how do do this in a gridview or detailsview but for some reason it isn't working on just a plain form.

So when a User who is not in the "Admin" role logs onto the page he either doesn't see  the button or the hyperlink has been disabled thus not allowing him to proceed any further.

 

Anyone got any ideas?

 

Any assistance would be greatly welcomed.

Here is an example that i would use:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Not User.IsInRole("Admin") Then
                Image1.Visible = False
            End If
        End If
    End Sub


 

or in c#:

protected void Page_Load(object sender, System.EventArgs e)
{
	if (!Page.IsPostBack) {
		if (!User.IsInRole("Admin")) {
			ImageButton1.Visible = false;
                        hyperlink1.visible = false;
		}
	}
}


 

Well that seemed to take care of it for the most part.  I do have 1 issue still, I actually have 4 Roles (Admin, Supervisor, PowerUser, User).  I can get each to work individually from each other is there a way that I could do something like this:

If not Page.IsPostBack then
If Not User.IsInRole("Admin") or (User.IsInRole("Supv") then
Image1.visible = false
End if
end if


  

 you can do this:

Protected Sub Page_Load(sender As Object, e As System.EventArgs)
	If Not Page.IsPostBack Then
		If (Not User.IsInRole("Admin")) OrElse (Not User.IsInRole("Supv")) Then
			ImageButton1.Visible = False
			hyperlink1.visible = False
		End If
	End If
End Sub


 

that doesn't seem to work at all, nothing is showing up.

Here's the code based of your sample:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If (Not User.IsInRole("Admin")) OrElse (Not User.IsInRole("GADS")) Then
                GADSItemButton.Visible = False
            End If
        End If
    End Sub


Any Ideas why this isn't working.  And "yes", GADS is a role.

 

I figured it out.  Thanks 

Did you check: there are any links/image for 'Admin' or 'GADS' role ? and also debug the code to see what it's actually returning....

thoughts on "[RESOLVED] Disable a Image button or Hyperlink button based on ASP.Net Role"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>