为什么会<%=%GT;前pressions的属性值在服务器控件导致编译错误? [英] Why will <%= %> expressions as property values on a server-controls lead to a compile errors?

查看:113
本文介绍了为什么会<%=%GT;前pressions的属性值在服务器控件导致编译错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这问题是在试图回答<一个后来我发现结果href=\"http://stackoverflow.com/questions/369736/assigning-visible-property-of-the-button-to-a-static-method-result\">another问题。而现在即时通讯好奇,想知道为什么&LT; ASP:文本框=服务器可见=&LT;%=真正%GT; /&GT; 导致编译错误,而不是为我本来期望一个可见的文本框

This question is a result of what i noticed while trying to answer another question. And now im curious to know why <asp:TextBox runat="server" Visible="<%= true %>" /> leads to a compile error, and not to a visible TextBox as i would have expected.

从我迄今发现的&LT;%=%&GT; 前pressions不翻译成文字的控件,因为我一直认为。但是,相反它是评估,当页面呈现直接写入的HtmlTextWriter。但很显然,分析器(我不确定那是因为这是翻译ASP.NET标记到.NET code中的一部分,正确的说法),甚至没有试图评估&LT;%=% &GT; 前pressions,当它们被用作服务器控件的属性值。它只是使用它作为一个字符串。我的猜测是,为什么我得到的错误信息:无法从它的字符串重新presentation创建类型的'System.Boolean'的对象;对于可见属性' EM>。

From what i have discovered so far, the <%= %> expressions are not translated to literal controls as i have always thought. But instead it is evaluated and written directly to the HtmlTextWriter when the page is rendered. But apparently the parser (I'm unsure that is the correct term for the part that is translating ASP.NET markup to .NET code) doesn't even attempt to evaluate <%= %> expressions, when they are used as property values for server controls. It just uses it as a string. Which i guess is why I get the error message: Cannot create an object of type 'System.Boolean' from its string representation '<%= true %>' for the 'Visible' property.

如果我不是沟=服务器,并结合了&LT;%=%&GT; 与普通的HTML标记,像这样的:

If i instead ditch the runat="server" and combines the <%= %> with regular html-markup, like this:

<input type="button" id="Button1" visible='<%= true %>' />

然后解析器刚刚分拆部分块之前和前pression后,然后在渲染方法写入的HtmlTextWriter。事情是这样的:

Then the parser just splits the chunk in parts before and after the expression and then writes it to the HtmlTextWriter in the render method. Something like this:

    __w.Write("<input type=\"button\" id=\"Button1\" visible='");
    __w.Write(true);
    __w.Write("' />");

随着最后一件事我注意到...当我尝试用&LT;%#%&GT; + Control.DataBind(),然后我得到了我所期望的。它挂钩之恩pression时要使用的控制数据绑定,但不像在&lt;%=%>前pression,产生的code实际评估&LT;%#%&GT; 前pression。解析器结束了生成以下内容:

As the last thing i noticed... When i try with <%# %> + Control.DataBind(), then i get what i would expect. It hooks up the expression to be used when the control is databound, but unlike the <%= %> expression, the generated code actually evaluates the content of the <%# %> expression. The parser ends up generating the following:

[DebuggerNonUserCode]
private Button __BuildControldataboundButton()
{
    Button button = new Button();
    base.databoundButton = button;
    button.ApplyStyleSheetSkin(this);
    button.ID = "databoundButton";
    button.DataBinding += new EventHandler(this.__DataBindingdataboundButton);
    return button;
}

public void __DataBindingdataboundButton(object sender, EventArgs e)
{
    Button button = (Button) sender;
    Page bindingContainer = (Page) button.BindingContainer;
    button.Visible = true;
}

从:

<asp:Button ID="databoundButton" Visible='<%# true %>' runat="server" />

注意 button.Visible = TRUE; 这是结果的&LT;%#%&GT; 前pression。

Notice the button.Visible = true; that is the result of the <%# %> expression.

我的问题是。为什么在第一个例子中的前pression只是当作一个字符串,而不是被评估为真。这位前pressions是两个其他的例子有些类似,它们产生code我本来期望。

So my question is.. Why is the expression in the first example just treated as a string, instead of being evaluated to "true". The expressions is somewhat similar for the two other examples, and they yield the code i would have expected.

难道仅仅是一个错误(我怀疑,因为它不符合ASP.NET当前版本的一个新的问题),或者是有我们为什么不允许使用&LT一个很好的理由;%=%GT; 一样,

Is it just a mistake (which i doubt since it isn't a new issue with the current version of ASP.NET), or is there a good reason why we are not allowed to use <%= %> like that?

推荐答案

<asp:Button runat="server" id="Button1" visible='<%= true %>' />

不计算为这样的:

Does not evaluate to this:

<asp:Button runat="server" id="Button1" visible='true' />

&下;%=%>直接输出到响应流,并在asp标记是不响应流的一部分。它是一种错误的假设在&lt;%=%>运营商都在ASP标记进行任何形式的preprocessing的

<%= %> outputs directly to the response stream, and the asp markup is not part of the response stream. Its a mistake to assume the <%= %> operators are performing any kind of preprocessing on the asp markup.


顺便说一句,它有助于想想ASP.NET生命周期方面的&lt;%#%>和&lt;%=%>运营商

As an aside, it helps to think about the ASP.NET lifecycle with respect to the <%# %> and <%= %> operators.


  • &LT;%#%>的语义共同更多的一个对象赋值。在ASP.NET生命周期中,&lt;%#%>运营商面前的页写入第一个字节响应缓冲区评估

  • <%# %> has semantics more in common with assigning a value to an object. In the ASP.NET lifecycle, the <%# %> operators are evaluated before the page writes the first byte to the response buffer.

&LT;%=%>表示同样的事情的Response.Write。我们需要先进行所有的数据绑定和表单处理,并输出到HTML在ASP.NET生命周期的最后响应缓冲区。

<%= %> means the same thing as Response.Write. We need to perform all of our databinding and form processing first, and output HTML to the response buffer at the very end of the ASP.NET lifecycle.

这篇关于为什么会&LT;%=%GT;前pressions的属性值在服务器控件导致编译错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆