问题描述
当我在数据绑定一整页,我会做这样的事情:
When I am databinding an entire page, I will do something like this:
等等等等...
<%# SomeProperty == "GoodBye" ? "See you later" : "Hello" %>
和的作品beatifully。但是,我常常不会使用数据绑定为整个页面,写东西了CLASICASP.NET的方式。例如,在code后面我会碰到这样的:
And that works beatifully. However, often I will not use databinding for an entire page and write things the "clasic" ASP.NET way. E.g., in the code behind I will have something like:
lblSomeMessage.Text = SomeProperty == "GoodBye" ? "See you later" : "Hello";
和则的.aspx将有
<asp:label runat="server" id="lblSomeMessage"/>
但我想要做的两个的......诸如此类的。我想这样做是不使用数据绑定语法,而是一个code座:
But what I want to do both...sort of. What I would like to do is not use databinding syntax but instead a code block:
<%= SomeProperty == "GoodBye" ? "See you later" : "Hello" %>
^^^^
诺埃输出标记语法。现在的问题是,什么时候这个标签实际评估?假如我没有设置SomeProperty属性,直到在preRender事件。是不是太晚了?在我的测试其实我这样做:
Noe the output tag syntax. Now, the question is, when will this tag actually be evaluated? Suppose I don't set the SomeProperty property until the OnPreRender event. Is that too late? In my testing I actually did this:
<%= SomeProperty == "GoodBye" ? + new System.Diagnostics.StackTrace().ToString() : "OH NO!" %>
和根据堆栈跟踪:
ASP.webform1_aspx .__ Renderform1(HtmlTextWriter的__w,控制
parameterContainer)在
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter的作家,
ICollection的孩子)的
System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter的
作家)...
它发生在渲染,这是完美的。但是,这是保证?任何陷阱这个规则?
It happens during render, which is perfect. But is this guaranteed? Any gotchas to this rule?
推荐答案
是的,它是保证是渲染
。
嵌入code块是服务器code,它在执行页面的呈现阶段。 - http://msdn.microsoft.com/en-us/library/ms178135.aspx
"An embedded code block is server code that executes during the page's render phase." - http://msdn.microsoft.com/en-us/library/ms178135.aspx
这篇关于当ASP.NET是code块,例如,&LT;%=%GT;在页面生命周期执行的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!