2012-03-07 72 views
51

我已经在.NET Framework 4的项目中使用服务器标签,如<%=无论%>设置的RUNAT =“服务器”控制的知名度,像下面的工作了:设置Visible属性与服务器标签<%= %> 3.5

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

这对框架4有效,但现在试图在Framework 3.5项目上使用它似乎不起作用。这是仅限于Framework 4的功能吗?有没有最酷的(和.aspx方)替代设置从代码隐藏的可见性?我使用的是丑陋的:提前

MiId.Visible = MyVisiblePropertyOnCodeBehind 

感谢,

汤姆

将帖子SOLUTION:

感谢您的意见,这让我明白我的问题和解!

这是我的错不止一件事。

在VS2010项目中,我们使用的替代<%=

而且<%#,我没有注意到的是,在VS2010项目中,我们使用的页面继承不是从“页面”,但是从CustomPage类,这是自动进行绑定,没有我注意到,这让我认为这是一个Framework 4.0的唯一功能。

照你说的这里,如果你有以下标记:

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

你可以把它的工作,加入以下代码隐藏:

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

当我读到这的DataBind( )可能会降低应用程序的性能。你有多少的想法?这是否可以理解为用于大型项目的“专业”技术,或者您认为应该避免这种技术?

我喜欢它在单一视图中使标记易读并易于理解的方式,但是我不想因为这样而对慢代码感到内疚。

+0

'MyVisiblePropertyOnCodeBehind'属性的签名是什么? – PraveenVenu 2012-03-07 07:01:43

+0

我其实不认为把它放在代码背后是不好的。如果您想提醒控件不一定可见,请在代码前面设置'visible =“false”'作为默认值。 – mac9416 2017-09-19 14:20:43

回答

69

您发布的代码不是在ASP.NET 2.0或ASP.NET 4.0运行时服务器标签有效的语法。在这两种版本,试图设置使用<%= ... %>可见属性中的服务器标签应导致一个解析错误:

Parser Error Message: Cannot create an object of type 'System.Boolean' from its string representation '<%=MyVisiblePropertyOnCodeBehind%>' for the 'Visible' property.

你不只是在设置代码隐藏的Visible属性或<script runat="server">标签其他两个选项。首先是使用Visible属性的数据绑定。您需要在MyId或其父控件之一上调用DataBind()方法以确定要绑定的值。

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

另一种选择是如下编写代码:

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

这种方法的缺点是,你将无法以编程控件添加到包含页面或控制代码块。如果你尝试,你应该得到一个错误:

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

之所以这么说,我认为只需设置属性你现在正在做的方式是要走的路。

+0

我终于明白我的问题了,因为这个以及“arunes”消息中的链接。谢谢。 (抱歉,但我是新的,不能投票)。 – tomasofen 2012-03-11 05:41:51

+0

很酷,很高兴我能帮忙! – rsbarro 2012-03-11 17:13:21

+1

@rsbarro我发现了另一个奇怪的地方,使用了一个' AaronLS 2013-01-09 18:01:59

3

As for ASP.NET aspx page's inline expression. <% %> 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 =<% %> ..>). As you've found you can create custom expression builder in ASP.NET 2.0 to add your inline expression.

BTW, another means for supplying values to server control properties in aspx inline tempalte is using <%# %> databinding expression. 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

全部张贴在这里:http://www.aspnet-answers.com/microsoft/ASP-NET/29389067/dynamically-set-a-control-property.aspx

而且这里的解决方法:ASP.net Inline Expression Issue

3

只需设置为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; 
    } 
} 

然后在aspx页面内控件的属性,通过属性设置自己的知名度就像

Visible="<%# !IsEditMode %>" 
+0

对不起,但这件事情与问题无关,而且代码很混乱,使用不同的属性名称:( – tomasofen 2012-03-11 15:09:29

+0

@tomasofen我已经用适当的命名更新了我的答案。 – 2014-08-28 10:19:36

1

这里另一种方法可以保持原始问题的简单性。在这里,你必须从div标签中删除runat =“server”,并使用css“display:none”而不是“Visible”属性。这种方法的缺点是标签仍然被发送到浏览器,即使它不可见并且可见性在客户端被处理。

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