1

In our master page, we are trying to hide the link to Admin screen on page load. Here is the code:

<div class="footer" id="divAdmin" Visible="False">
  <ul>
   <li><a href="~/admin.aspx">Administration Page</a></li>
  </ul>
</div>

After the user successfully logs in, show the link if the user is an admin. An admin has userRole of 1 assigned to it.

I am trying to accomplish this with code below on page_load() event:

If Session("UserRole") = 1 Then
 divAdmin.Visible = True
End If

I can't get beyond the error the following error:

Error 'divAdmin' is not declared. It may be inaccessible due to its protection level.

Any ideas how to fix this?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Chidi Okeh
  • 1,537
  • 8
  • 28
  • 50

1 Answers1

3

Set Visible to False in markup. Add runat="server" to the element. Set Visible to True in code-behind once authenticated.

<div class="footer" id="divAdmin" Visible="False" runat="server">
  <ul>
   <li><a href="~/admin.aspx">Administration Page</a></li>
  </ul>
</div>


If Session("UserRole") = 1 Then
 divAdmin.Visible = True
End If
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
  • That's exactly the way my code is written. As stated, I cannot get beyond the error that `divAdmin is not declared` – Chidi Okeh Jun 13 '13 at 17:33
  • 1
    You don't have runat="server". See http://stackoverflow.com/a/11510589/397817 for more info. – Stephen Kennedy Jun 13 '13 at 17:34
  • This link may be more helpful. See the section "ASP.NET - HTML Server Controls". http://www.w3schools.com/aspnet/aspnet_controls.asp – Stephen Kennedy Jun 13 '13 at 17:38
  • Sorry about that @Stephen. I must have mistakenly taken the runat="server" out just trying and pasting from one section of the master page to another hoping to get it to work. Right now though, the error is gone but is still doesn't show the link after successful login and I have a userRole of 1. Ok, I see it now. I had to add Handles Me.Load to page_load() event. Thank you for your prompt assistance. – Chidi Okeh Jun 13 '13 at 17:43