1

Right, please don't point me there: Disable a HyperLink from code behind

My problem is as follows. I have a hyperlink on my aspx page:

<asp:HyperLink Visible='<%= _myUser.hasPermission("Intranet Management")%>' Text="Intranet Management" runat="server" NavigateUrl="/Apps/Admin/Default.aspx" />

_myUser.hasPermission("Intranet Management") returns boolean with value of TRUE or FALSE depends if a current user has that permission or not. _myUser is declared in aspx.cs file as protected member so I am able to access it from aspx file.

On my page I am getting following error:

Parser Error Message: Cannot create an object of type 'System.Boolean' from its string representation '<%= _myUser.hasPermission("Intranet Management") %>' for the 'Visible' property.

Is there any other way of doing that in aspx file? Please don't ask me to do it in the code behind, I have my reasons to do it here...

Thanks for any help.

Community
  • 1
  • 1
Daniel Gruszczyk
  • 5,379
  • 8
  • 47
  • 86

4 Answers4

4

The problem you're facing is that asp:Hyperlink is a server control, and these wont evaluate code inside <%= %> for their properties. They will databind though IIRC, so you could try

<asp:HyperLink Visible='<%# _myUser.hasPermission("Intranet Management")%>'...

And make sure to call Page.DataBind().

Jamiec
  • 133,658
  • 13
  • 134
  • 193
3

In order to do it this way, you cannot have runat="server". The idea is that server-side controls will be modified using code behind.

If you don't want to use code behind, use a regular <a> tag without runat="server". There doesn't seem to really be any reason why you need a server control here anyway.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
1

use the following instead:

<%# _myUser.hasPermission("Intranet Management") %>

Got it from here

Community
  • 1
  • 1
Arief
  • 6,055
  • 7
  • 37
  • 41
1

This should work ...

<asp:HyperLink ID="HyperLink1" Visible='<%# Convert.ToBoolean(_myUser.hasPermission("Intranet Management")) %>' Text="Intranet Management" runat="server" NavigateUrl="/Apps/Admin/Default.aspx" />

Perhaps this is better after all

<a style='<%= Convert.ToBoolean(_myUser.hasPermission("Intranet Management")) ? "" : "display:none;" %>' href="Apps/Admin/Default.aspx"> Intranet Management </a>
Simon Dugré
  • 17,980
  • 11
  • 57
  • 73