[英]Access a control in Codebehind that's in a Template
In my ASP.NET WebForms application using Scafolding I have many pages where I need to restrict certain links based on user's role. 在使用脚手架折叠的ASP.NET WebForms应用程序中,我有很多页面需要根据用户角色限制某些链接。 For instance, in my Site.Master in my <LoggedTemplate>
along with other <li>
, I have a <li>
for Admin page also. 例如,在我的<LoggedTemplate>
Site.Master和其他<li>
,我也有一个<li>
用于管理页面。 By default, that is not visible, but if the user is logged as an Admin, then I want it to make visible. 默认情况下,这是不可见的,但是如果用户以管理员身份登录,那么我希望它变得可见。 Which I am not able to do. 我无法做到。 Here's the code for it : 这是它的代码:
<LoggedInTemplate>
<ul class="nav navbar-nav">
<li><a runat="server" id="adminLink" visible="false" href="~/Admin/Admin_Page">Admin</a></li>
<li><a runat="server" href="~/Inquiries/Default.aspx">Inquiry</a></li>
In my Codebehind, in Page_Load
I am not able to access adminLink
only. 在我的代码隐藏中,在Page_Load
我无法仅访问adminLink
。
Simialrly, in one of the Default
page of a Model
, the list has links to View, Insert & Delete. 同样,在“ Model
的“ Default
页面之一中,列表具有指向“查看,插入和删除”的链接。 If the user is admin, then only I want to show Insert & Delete links. 如果用户是admin,则只有我要显示“插入和删除”链接。 Here's the code for it : 这是它的代码:
<td>
<asp:HyperLink runat="server" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Details", Item.ChannelId) %>' Text="View" /> |
<asp:HyperLink runat="server" ID="editLink" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Edit", Item.ChannelId) %>' Text="Edit" /> |
<asp:HyperLink runat="server" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Delete", Item.ChannelId) %>' Text="Delete" />
</td>
</tr>
</ItemTemplate>
I tried adding 我尝试添加
<% if (CommonUtilities.IsUserAdmin) { %>
hyperlinks for Insert & delete & finally
<% } %>
but this was giving error. 但这给了错误。 I added ID
to editLink
, but again cannot access it in Page_Load
method. 我将ID
添加到editLink
,但再次无法在Page_Load
方法中访问它。
I am sure, their must be some method to work out with this which I am not able to find yet. 我确定,他们必须采用某种方法来解决此问题,而我目前还无法找到。 How to deal with this problem ?? 如何处理这个问题? Please help me, I have several pages & links to hide & show based on admin role. 请帮助我,我有几个页面和链接根据管理员角色隐藏和显示。
Any help is highly appreciated. 非常感谢您的帮助。
Thanks 谢谢
I think you are looking for FindControl
. 我认为您正在寻找FindControl
。 For example: 例如:
Label adminLabel = LoggedInTemplate.FindControl("adminLink") as Label;
adminLabel.visible = true;
Works for me in a few templates, dont know about LoggedInTemplate tho, but can't see why not. 在一些模板中为我工作,不了解LoggedInTemplate,但是看不到为什么。
edit: didn't realize ur using <a>
. 编辑:没有意识到您使用<a>
。 Not sure why you mix asp hyperlink and html but anyway, logic is still the same. 不知道为什么要混用asp超链接和html,但是无论如何,逻辑仍然是相同的。
Thanks WEDEBE for pointing out to mix <a>
and . 感谢WEDEBE指出混合<a>
和。 That point gave me a way out. 那一点给了我一个出路。 In my design, I change <a>
to <asp:HyperLink>
& removed code from Codebehind. 在我的设计中,我将<a>
更改为<asp:HyperLink>
并从Codebehind中删除了代码。 In design only I tried checking hte role of user & then adding full <li>
. 仅在设计中,我尝试检查用户的角色,然后添加完整的<li>
。 Like this : 像这样 :
<LoggedInTemplate>
<ul class="nav navbar-nav">
<% if (VincitoreCRMApplication.CommonUtilities.IsCurrentUserAdmin)
{ %>
<li> <asp:HyperLink runat="server" id="adminLink" NavigateUrl="~/Admin/Admin_Page.aspx">Admin</asp:HyperLink> </li>
<% } %>
<li><a runat="server" href="~/Inquiries/Default.aspx">Inquiry</a></li>
With the other 2 HyperLink's also I did the same way : 对于其他2个HyperLink,我也使用相同的方法:
<% if (VincitoreCRMApplication.CommonUtilities.IsCurrentUserAdmin)
{ %>
<asp:HyperLink runat="server" ID="editLink" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Edit", Item.ChannelId) %>' Text="Edit" /> |
<asp:HyperLink runat="server" NavigateUrl='<%# FriendlyUrl.Href("~/Channels/Delete", Item.ChannelId) %>' Text="Delete" />
<% } %>
And this worked. 这行得通。 But I realized one thing, when If I add 但是当我添加
<% if (VincitoreCRMApplication.CommonUtilities.IsCurrentUserAdmin) { %>
like this, in a single line, it's not working. 像这样,在一行中不起作用。 But on adding the curly braces in new line, it works. 但是在将花括号添加到新行中时,它可以工作。 I know this sounds very strange, I also can't make out why it happens so. 我知道这听起来很奇怪,我也不知道为什么会这样。 But it is fact, that what I have faced & learned. 但事实是,我所面对和了解到的。
I know this is quiet simple thing, but just in case my code helps anyone, have shared here. 我知道这是一件很简单的事情,但是以防万一我的代码可以帮助任何人,请在这里共享。
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.