0

On my site master page I have navigation code that has links that I want to restrict visibility for. Here is the HTML to the point where I am having issues:

<%@ Master Language="C#" AutoEventWireup="True" CodeBehind="Site.master.cs" Inherits="MAMOnline.Site" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>MAMOnline - <%: Page.Title %></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="Stylesheets" runat="server">
    <link rel="stylesheet" href="/css/reset.css" type="text/css" />
    <link rel="stylesheet" href="/css/styles.css" type="text/css" />
</asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server">
        <Scripts>
<%--Framework Scripts--%>
            <asp:ScriptReference Name="jquery" />
            <%--Site Scripts--%>

    </Scripts>
</asp:ScriptManager>
<div id="container">

<header> 
<div class="width">

        <h1><a href="/">MAM<strong>Online</strong></a></h1>

    <nav>

            <ul class="sf-menu dropdown">
                <asp:LoginView runat="server" ViewStateMode="Disabled">
                    <LoggedInTemplate>

                        <li class="selected"><a runat="server" href="~/Default.aspx"><i class="fa fa-home"></i> Home</a></li>


                        <li><a runat="server" href="~/MyAccount.aspx"><i class="fa fa-briefcase"></i> My Account</a></li>
                        <li><a runat="server" href="~/Admin/Default.aspx" id="adminLink" Visible="False"><i class="fa fa-briefcase"></i> Administration</a></li>

                        <li><a id="logoutLink" runat="server" href="~/AuthHandler.ashx?logout=true"><i class="fa fa-phone"></i>Log out</a></li>
                    </LoggedInTemplate>
                    </asp:LoginView>
            </ul>

I am trying the element with the id "adminLink". However, it does not appear in the designer file and referencing it in the .cs code behind file does not work. Am I making an obvious mistake?

2 Answers2

0

If you want to use ASP.NET control in code behind file or designer, try change this

<a runat="server" href="~/Admin/Default.aspx" id="adminLink" Visible="False"><i class="fa fa-briefcase"></i> Administration</a>

To

<asp:HyperLink runat="server" .... id="adminLink" ..../>
display name
  • 4,165
  • 2
  • 27
  • 52
  • 1
    Unfortunately this doesn't work. The solution was to use `LoginView.FindControl("adminLink")` to gain code access to the control. – user3727001 Dec 05 '14 at 17:47
0

This is my solution: controls are relocated outside the template, set the runat="server" id="adminLink" properties on parent control <li>

<ul class="sf-menu dropdown">
<li class="selected"><a runat="server" href="~/Default.aspx"><i class="fa fa-home"></i>Home</a></li>
<li><a runat="server" href="~/MyAccount.aspx"><i class="fa fa-briefcase"></i>My Account</a></li>
<li runat="server" id="adminLink"><a href="~/Admin/Default.aspx"><i class="fa fa-briefcase"></i>Administration</a></li>
<asp:LoginView runat="server" ViewStateMode="Disabled">
    <LoggedInTemplate>
        <li><a id="logoutLink" runat="server" href="~/AuthHandler.ashx?logout=true"><i class="fa fa-phone"></i>Log out</a></li>
    </LoggedInTemplate>
</asp:LoginView>

then in code behind you can set visible property for "adminLink", for instance

adminLink.Visible = Context.User.IsInRole("4");