I am trying to hide the button based on the user’s role using the following code:
<asp:Button ID='btndisplayrole' Text='Admin Button' Visible='<%= WebApplication1.SiteHelper.IsUserInRole('Admin') %>' runat='server' OnClick='DisplayRoleClick' />
But when I run the above code I get the following error message:
Cannot create an object of type ‘System.Boolean’ from its string representation ‘<%= WebApplication1.SiteHelper.IsUserInRole(‘Admin’) %>’ for the ‘Visible’
Kind of an interesting issue.. But as the error message states, the string
<%= WebApplication1.SiteHelper.IsUserInRole('Admin') %>cannot be converted to a boolean.Unfortunately i cannot explain why the expression isn’t evaluated, but instead is treated like a string.
The reason why your
<%# %>expression works as expected, is because it is treated much differently. When the Page is compiled into a class, then the compiler creates an event handler similar to this:and hooks this method up to the Control.Databinding event on your control. As you can see, the
<%# %>is this time properly treated as server code, and not just a random string.So i guess the solution is either to use databinding, or go to the codebehind as AndreasKnudsen suggests.