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:
<div id="MyId" runat="server" visible="<%=MyVisiblePropertyOnCodeBehind %>" >
Content
</div>
这在Framework4上是有效的,但现在try 在Framework3.5项目上使用它似乎不起作用.这是Framework4独有的功能吗?除了从codebehind设置可见性,还有最酷的(和.aspx方面)替代方案吗?我用的是丑陋的:
MiId.Visible = MyVisiblePropertyOnCodeBehind
提前谢谢你,
汤姆
[EDITED] SOLUTION:
感谢您的 comments ,让我了解我的问题和解决方案!
不止一件事是我的错.
在VS2010项目中,我们使用了<;%#而不是<;%=
此外,我没有注意到在VS2010项目中,我们使用的页面不是从"Page"继承的,而是从CustomPage类继承的,这是自动进行绑定的,我没有注意到它,这让我认为这只是Framework4.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()会降低应用程序的性能.你知道多少钱吗?这可以理解为在大型项目中使用的"专业"技术吗?或者你认为应该避免这种做法吗?
我喜欢它使标记在单一视图中可读且易于理解的方式,但我不想因为代码太慢而感到内疚.