I'm trying to set the Visible property of a div with runat='server'开发者_JS百科 attribute, directly from the code in the page, by calling a function in the code behind, but it seems the code is not evaluated at all by the server. The code looks like this
<div id="mydiv" runat="server" Visible='<%# IsInRole("User") %>'>
Where 'IsInRole' is a protected function on the page that return a bool.
Many Thanks
FOUND SOLUTION:
<% if (IsInRole("Administrator")){%>
<div id="mydiv">Admin content here</div>
<% }%>
As the div is runat="server" you should be able to access it from code behind something like this in pageload:
mydiv.Visible = IsInRole(User);
(If IsInRole returns true or false)
In
<div id="mydiv" runat="server" Visible='<%# IsInRole("User") %>'>
# indicates a databinding, and its content will be evaluated only at the moment the databinding is done. So first, be sure, your control is databound during the execution of the page, and secondly that your value is correctly evaluated at that time.
otherwise you might prefer
<div id="mydiv" runat="server" Visible='<%= IsInRole("User") %>'>
Where your espression will be evaluated on every execution of the page.
see this blog for further details
精彩评论