为什么会 <%= %>作为服务器控件上的属性值的表达式会导致编译错误? [英] Why will &lt;%= %&gt; expressions as property values on a server-controls lead to a compile errors?

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

问题描述

这个问题是我在尝试回答时注意到的结果 另一个问题.现在我很想知道为什么 <asp:TextBox runat="server" Visible="<%= true %>"/> 导致编译错误,而不是我预期的可见 TextBox.

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.

从我目前所发现的情况来看,<%= %> 表达式并没有像我一直认为的那样转换为文字控件.但是,当页面呈现时,它会被评估并直接写入 HtmlTextWriter.但显然解析器(我不确定这是将 ASP.NET 标记转换为 .NET 代码的部分的正确术语)甚至没有尝试评估 <%= %>表达式,当它们用作服务器控件的属性值时.它只是将它用作字符串.我想这就是我收到错误消息的原因:无法从可见"属性的字符串表示<%= true %>"创建System.Boolean"类型的对象.

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.

如果我放弃 runat="server" 并将 <%= %> 与常规 html 标记相结合,如下所示:

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

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

然后解析器只是将块在表达式之前和之后分成几部分,然后将其写入渲染方法中的 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("' />");

作为我注意到的最后一件事......当我尝试使用 <%# %> + Control.DataBind() 时,我得到了我所期望的.当控件是数据绑定时,它会连接要使用的表达式,但与 <%= %> 表达式不同,生成的代码实际上计算 <%# %> 表达式的内容.解析器最终生成以下内容:

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; 这是 <%# %> 表达式的结果.

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

所以我的问题是.. 为什么第一个示例中的表达式只是被视为字符串,而不是被评估为true".其他两个示例的表达式有些相似,它们产生了我预期的代码.

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 的新问题),还是有充分的理由不允许我们使用 <%=%> 像那样吗?

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 %>' />

不评估:

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

<%= %> 直接输出到响应流,asp 标记不是响应流的一部分.假设 <%= %> 运算符对 asp 标记执行任何类型的预处理是错误的.

<%= %> 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 生命周期会有所帮助.

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

  • <%# %> 与为对象赋值的语义更相似.在 ASP.NET 生命周期中,<%# %> 运算符在页面将第一个字节写入响应缓冲区之前进行评估.

  • <%# %> 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.

<%= %> 与 Response.Write 的意思相同.我们需要首先执行所有数据绑定和表单处理,并在 ASP.NET 生命周期的最后将 HTML 输出到响应缓冲区.

<%= %> 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.

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

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