user8142888
user8142888

Reputation:

Display / Hide Advert Based on Role

I created a Web Control which is basically a div for holding a mobile banner but I wish to hide it if the user is an Admin

I have tried:

<mob:MobileBanner runat="server" ID="MobileBanner" Visible='<%# Not HttpContext.Current.User.IsInRole("Admin") %>' />

but it doesn't seem to work.

I was also thinking of surrounding the html but cannot seem to get that working so it checks if you're an admin and if not it writes the advert code to the client, which includes the css link.

There is a similar question (how to enable and disable button based on user role?) but that is an ASP.NET Server Side button not a basic Web Control which just holds html.

Those of you who want code, add a new Web Control to the project then paste in this code.

<link href = "my_css_file_here.css" rel="stylesheet" type="text/css" />

<div Class="my_banner_container">
    <!--My banner code goes here -->
</div>

I know I could create a container in the master document with runat="server" then use:

my_container.Visible = Not HttpContext.Current.User.IsInRole("Admin")

but I also need to change the padding-top: 130px; to padding-top: 50px; and swap back if the admin logs out or the user is not an admin.

I know if could have a link like this for non Admins:

<link href = "my_css_file_130_here.css" rel="stylesheet" type="text/css" runat="server" />

which would then become for Admins:

<link href = "my_css_file_50_here.css" rel="stylesheet" type="text/css" runat="server" />

then from code behind have 2 different css for the padding-top and change the href based on if the user is an admin or not

body {
     padding-top: 130px;
}

or

body {
     padding-top: 50px;
}

Any ideas on how to achieve this or a better way than my complicated idea?

Upvotes: 0

Views: 144

Answers (2)

user8142888
user8142888

Reputation:

What I did in the end was to add the link for the stylesheet and make it runat="server" with the href set to the 120px padding

In form_load (outside of PostBack checking so always checked) of the master doc:

If HttpContext.Current.User.IsInRole("Admin") Then
    my_container.Visible = False
    my_stylesheet.Href = "~/path_to_file50.css"
Else
    my_container.Visible = True
    my_stylesheet.Href = "~/path_to_file120.css"
End If

In those css files I set the

padding-top: 50px;

or

padding-top: 120px;

then left the rest of css in another file and all seems to be working now and by setting:

my_container.Visible = False

should stop Google complaining that I am creating impressions whilst on my own site.

Upvotes: 0

CodingYoshi
CodingYoshi

Reputation: 26989

Do not render it if you do not want it. Below will only render it if user is not in Admin role.

<%If Not HttpContext.Current.User.IsInRole("Admin")
Then
    <mob:MobileBanner runat="server" ID="MobileBanner" />
End If %>

Upvotes: 0

Related Questions