简体   繁体   English

在 Framework 3.5 中使用服务器标记 <%= %> 设置 Visible 属性

[英]Set Visible property with server tag <%= %> in Framework 3.5

I have been working in a .NET Framework 4 project using server tags like <%=whatever %> to set the visibility of runat="server" controls, like the following:我一直在一个 .NET Framework 4 项目中工作,使用诸如 <%=whatever %> 之类的服务器标记来设置 runat="server" 控件的可见性,如下所示:

  <div id="MyId" runat="server" visible="<%=MyVisiblePropertyOnCodeBehind %>" >
    Content
  </div>

This works on framework 4, but now trying to use this on a Framework 3.5 project it doesn't seems to work.这适用于框架 4,但现在尝试在框架 3.5 项目上使用它似乎不起作用。 Is this a Framework 4 only feature?这是 Framework 4 的唯一功能吗? Is there a coolest (and .aspx side) alternative to setting the visibility from codebehind?是否有最酷的(和 .aspx 方面)替代方法来设置代码隐藏的可见性? I'm using the ugly:我正在使用丑陋的:

    MiId.Visible = MyVisiblePropertyOnCodeBehind

Thanks in advance,提前致谢,

Tom汤姆

[EDITED] SOLUTION: [编辑] 解决方案:

Thanks for your comments that makes me understand my problem and the solution!感谢您的评论,让我了解我的问题和解决方案!

It was my fault in more than one thing.不止一件事是我的错。

In the VS2010 project we were using <%# instead of <%=在 VS2010 项目中,我们使用 <%# 而不是 <%=

Also, I didn't notice that in the VS2010 project we were using pages inherited not from “Page”, but from a CustomPage class, that was making the binding automatically, without me noticing it, and that makes me think that was a Framework 4.0 only feature.另外,我没有注意到在 VS2010 项目中我们使用的页面不是从“Page”继承的,而是从 CustomPage 类继承的,这会自动进行绑定,而我没有注意到,这让我认为这是一个框架仅 4.0 功能。

As you told here, if you have the following markup:正如您在这里所说,如果您有以下标记:

  <div id="MyId" runat="server" visible="<%# MyVisiblePropertyOnCodeBehind %>" >
    Content
  </div>

you can make it work, adding the following to the codebehind:你可以让它工作,在代码隐藏中添加以下内容:

    public bool  MyVisiblePropertyOnCodeBehind = true;
    protected void Page_Load(object sender, EventArgs e) {
        DataBind();
        // Or if you want only for one control, MyId.DataBind();             
    }

As I read, this DataBind() can reduce performance of the application.正如我所读到的,这个 DataBind() 会降低应用程序的性能。 Do you have idea of how much?你知道多少钱吗? Could this be understood as a “professional” technique to be used on big projects, or do you think it should be avoided?这是否可以理解为用于大型项目的“专业”技术,或者您认为应该避免使用它?

I love the way it makes markup readable and easy to understand in a single view, but I wouldn't like to be guilty of slow code because that.我喜欢它使标记在单个视图中可读且易于理解的方式,但我不希望因此而对缓慢的代码感到内疚。

The code you posted is not valid syntax for server tags in the ASP.NET 2.0 or ASP.NET 4.0 runtimes.您发布的代码不是 ASP.NET 2.0 或 ASP.NET 4.0 运行时中服务器标记的有效语法。 In either version, trying to set the visible property using <%= ... %> in a server tag should result in a parser error:在任一版本中,尝试在服务器标记中使用<%= ... %>设置可见属性都会导致解析器错误:

Parser Error Message: Cannot create an object of type 'System.Boolean' from its string representation '<%=MyVisiblePropertyOnCodeBehind%>' for the 'Visible' property.解析器错误消息:无法从“可见”属性的字符串表示形式“<%=MyVisiblePropertyOnCodeBehind%>”创建“System.Boolean”类型的对象。

You have two options other than just setting the Visible property in the codebehind or a <script runat="server"> tag.除了在代码隐藏或<script runat="server">标记中设置Visible属性之外,您还有两个选项。 The first is to use a databinding on the Visible property.第一种是在Visible属性上使用数据绑定。 You'll need to call the DataBind() method on either MyId or one of its parent controls for the value to be bound.您需要在MyId或其父控件之一上调用DataBind()方法来绑定值。

<div id="MyId" runat="server" visible="<%# MyVisiblePropertyOnCodeBehind %>" >
    Content
</div>

The other option is to write the code as follows:另一种选择是编写如下代码:

<% if(MyVisiblePropertyOnCodeBehind) { %>
<div id="MyId" runat="server">
    Content
</div>
<% } %>

The disadvantage of this approach is that you won't be able to programmatically add controls to the page or control that contains the code blocks.这种方法的缺点是您将无法以编程方式向页面或包含代码块的控件添加控件。 If you try to you should get an error:如果你尝试你应该得到一个错误:

The Controls collection cannot be modified because the control contains code blocks (ie <% ... %>)无法修改 Controls 集合,因为该控件包含代码块(即 <% ... %>)

All that being said, I think just setting the property the way you are doing it now is the way to go.话虽如此,我认为按照您现在的方式设置属性是可行的方法。

As for ASP.NET aspx page's inline expression.至于 ASP.NET aspx 页面的内联表达式。 <% %> can only be used at aspx page or user control's top document level, but can not be embeded in server control's tag attribute (such as <asp:Button... Text =<% %> ..> ). <% %> 只能用于 aspx 页面或用户控件的顶级文档级别,但不能嵌入服务器控件的标签属性中(例如<asp:Button... Text =<% %> ..> )。 As you've found you can create custom expression builder in ASP.NET 2.0 to add your inline expression.正如您发现的,您可以在 ASP.NET 2.0 中创建自定义表达式生成器来添加内联表达式。

BTW, another means for supplying values to server control properties in aspx inline tempalte is using <%# %> databinding expression.顺便说一句,另一种为 aspx 内联模板中的服务器控件属性提供值的方法是使用 <%# %> 数据绑定表达式。 This is built-in supported.这是内置支持的。 The only different from other inline expression is that method on the target control or its Container control.与其他内联表达式唯一不同的是目标控件或其容器控件上的方法。

Steven Cheng史蒂文郑

Microsoft MSDN Online Support Lead Microsoft MSDN 在线支持主管

Full post here: http://www.aspnet-answers.com/microsoft/ASP-NET/29389067/dynamically-set-a-control-property.aspx全文在这里: http : //www.aspnet-answers.com/microsoft/ASP-NET/29389067/dynamically-set-a-control-property.aspx

And workaround here: ASP.net Inline Expression Issue解决方法: ASP.net Inline Expression Issue

Here is another approach which maintains the simplicity of the code from your original question.这是另一种方法,它保持原始问题中代码的简单性。 Here you would have to remove the runat="server" from the div tag and use css "display:none" instead of the "Visible" property.在这里,您必须从 div 标签中删除 runat="server" 并使用 css "display:none" 而不是 "Visible" 属性。 The downside to this approach is that the tag still gets sent to the browser even if it is not visible and the visibility is handled on the client side.这种方法的缺点是标签仍然会被发送到浏览器,即使它不可见并且可见性是在客户端处理的。

<div style='<%=MyVisiblePropertyOnCodeBehind ? "" : "display: none" %>' >
    Content
</div>

Just set a variable to true/false on your pageLoad event like this只需在您的 pageLoad 事件上将变量设置为 true/false,就像这样

private bool IsEditMode {get; set;}      

protected bool IsVisible 
{
    get { retun IsEditMode ;}
    set { IsEditMode =value;}
}  

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // based on some condition set this to true or false 
        isEditMode=true;
    }
}   

Then in your control properties inside aspx page, set their visibility via a property like然后在 aspx 页面内的控件属性中,通过类似的属性设置它们的可见性

Visible="<%# !IsEditMode %>" 

Another approach is ti give an ID to the control and set the visible in code.另一种方法是给控件一个 ID 并在代码中设置可见。

protected void Page_Load(object sender, EventArgs e){
   MyId.Visible = MyVisiblePropertyOnCodeBehind;
} 

<div id="MyId" runat="server">Content</div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM