[英]Set Visible property of div with an inline-tag
I'm trying to set the Visible property of a div with runat='server' 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. 我试图通过调用后面的代码中的函数直接从页面中的代码中设置带有runat ='server'属性的div的Visible属性,但是似乎服务器根本不评估该代码。 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. 其中“ IsInRole”是页面上受保护的函数,返回布尔值。
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: 由于div是runat =“ server”,因此您应该能够从pageload中类似下面的代码中访问它:
mydiv.Visible = IsInRole(User); mydiv.Visible = IsInRole(User);
(If IsInRole returns true or false) (如果IsInRole返回true或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. 页面每次执行时将评估您的表达的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.