简体   繁体   English

使用内联标签设置div的Visible属性

[英]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. 页面每次执行时将评估您的表达的位置。

see this blog for further details 请参阅此博客以获取更多详细信息

带有 XML 标签的 POST 属性被替换为<div>在 controller?</div><div id="text_translate"><p> 当我将我的 model 发布到 controller 时,其属性包含一个 XML 标签,如 <web.config>,它是 controller 中的一个 div,为什么? (在浏览器的 devtools 中,postdata 仍然是正确的)</p><p> 所见物业:</p><pre> [AllowHtml] [Display(Name = "DataConfig")] public string DataConfig { get; set; }</pre><p> controller:</p><pre> [HttpPost] public ActionResult Edit(MyModel data) { //Here data.DataConfig contains the <div> instead of <web.config> return Redirect("myRedirct"); }</pre><p> 我想发布带有 fe 等随机标签的纯文本,这是视图中的 TextAreaFor: <a href="https://i.stack.imgur.com/eAo2v.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eAo2v.png" alt="在此处输入图像描述"></a></p><p> 注意表单数据: <a href="https://i.stack.imgur.com/nUIeM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nUIeM.png" alt="在此处输入图像描述"></a></p><p> 结果,两个 div.. <a href="https://i.stack.imgur.com/yHXeU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yHXeU.png" alt="在此处输入图像描述"></a></p></div> - POST property with XML tag is replaced to a <div> in controller?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将查询字符串结果绑定到服务器标签并设置按钮的可见属性 - Bound query string result to server tag and set visible property of button div标签的visible属性不起作用 - div tag visible attribute is not working 按钮切换div可见属性 - Button toggle div visible property 设定宽度 <div> 标签 - Set Width of <div> tag 在jQuery中设置DIV标签可见性 - Set DIV tag visibility in jquery div从.net设置可见的false - div set visible false from .net asp:TableRow设置可见属性不起作用 - asp:TableRow set visible property not working 从子触发器设置父 Visible 属性 - Set parent Visible property from child trigger 带有 XML 标签的 POST 属性被替换为<div>在 controller?</div><div id="text_translate"><p> 当我将我的 model 发布到 controller 时,其属性包含一个 XML 标签,如 <web.config>,它是 controller 中的一个 div,为什么? (在浏览器的 devtools 中,postdata 仍然是正确的)</p><p> 所见物业:</p><pre> [AllowHtml] [Display(Name = "DataConfig")] public string DataConfig { get; set; }</pre><p> controller:</p><pre> [HttpPost] public ActionResult Edit(MyModel data) { //Here data.DataConfig contains the <div> instead of <web.config> return Redirect("myRedirct"); }</pre><p> 我想发布带有 fe 等随机标签的纯文本,这是视图中的 TextAreaFor: <a href="https://i.stack.imgur.com/eAo2v.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eAo2v.png" alt="在此处输入图像描述"></a></p><p> 注意表单数据: <a href="https://i.stack.imgur.com/nUIeM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nUIeM.png" alt="在此处输入图像描述"></a></p><p> 结果,两个 div.. <a href="https://i.stack.imgur.com/yHXeU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yHXeU.png" alt="在此处输入图像描述"></a></p></div> - POST property with XML tag is replaced to a <div> in controller? 设置为可见时,仅显示错误div? - Make error div only shown when set to visible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM