根据角色

时间:2017-07-09 00:56:54

标签: html css asp.net vb.net asp.net-webcontrol

我创建了一个Web控件,它基本上是一个用于存放移动横幅的div,但如果用户是管理员,我希望隐藏它

我试过了:

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

但它似乎不起作用。

我还在考虑围绕html,但似乎无法正常工作,因此它会检查您是否是管理员,如果不是,它会将广告代码写入客户端,其中包括css链接。

有一个类似的问题(how to enable and disable button based on user role?),但这是一个ASP.NET服务器端按钮,而不是一个只保存html的基本Web控件。

那些想要代码的人,将新的Web Control添加到项目中,然后粘贴到此代码中。

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

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

我知道我可以使用runat =“server”在主文档中创建一个容器,然后使用:

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

但我还需要更改padding-top:130px;填充顶部:50px;如果管理员注销或用户不是管理员,则交换回来。

我知道如果非管理员可以有这样的链接:

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

然后将成为管理员:

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

然后从代码后面有两个不同的填充顶部的css,并根据用户是否是管理员更改href

body {
     padding-top: 130px;
}

body {
     padding-top: 50px;
}

关于如何实现这个或者比我复杂的想法更好的方法的任何想法?

2 个答案:

答案 0 :(得分:0)

如果您不想要它,请不要渲染它。以下内容仅在用户不在Admin角色时才会呈现。

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

答案 1 :(得分:0)

我最后做的是添加样式表的链接,并将其设置为120px填充的runat =“server”

在主文档中的form_load(在PostBack检查之外,因此总是检查):

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

在那些css文件中我设置了

padding-top: 50px;

padding-top: 120px;

然后将剩下的css留在另一个文件中,现在所有css似乎都在工作并设置:

my_container.Visible = False

应该阻止Google抱怨我在自己的网站上创建了展示次数。

相关问题