0

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

我努力了:

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

但它似乎不起作用。

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

有一个类似的问题(如何根据用户角色启用和禁用按钮?),但这是一个 ASP.NET 服务器端按钮,而不是仅包含 html 的基本 Web 控件。

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

<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; 到 padding-top: 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" />

然后从后面的代码中为 padding-top 提供 2 个不同的 css,并根据用户是否是管理员来更改 href

body {
     padding-top: 130px;
}

或者

body {
     padding-top: 50px;
}

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

4

2 回答 2

0

如果您不想要它,请不要渲染它。Admin如果用户不在角色中,下面只会呈现它。

<%If Not HttpContext.Current.User.IsInRole("Admin")
Then
    <mob:MobileBanner runat="server" ID="MobileBanner" />
End If %>
于 2017-07-09T01:17:12.223 回答
0

我最后所做的是添加样式表的链接并使其 runat="server" 与 href 设置为 120px 填充

在主文档的 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留在另一个文件中,现在一切似乎都在工作,并通过设置:

my_container.Visible = False

应该阻止谷歌抱怨我在自己的网站上创造了印象。

于 2017-07-09T02:15:00.867 回答