为什么<%=%GT;工作在一种情况下,但不是在另一 [英] Why <%= %> works in one situation but not in another

查看:131
本文介绍了为什么<%=%GT;工作在一种情况下,但不是在另一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从坏的答案,我昨晚给制止。好奇,为什么一种方法的工作原理,而不是其他的缠着我,我希望有人比我聪明可以给我正确的解释(或指向我的文档)为什么出现以下行为,因为它是。

This is stemming from a bad answer I gave last night. The curiosity as to why one method works and not the other is bugging me and I'm hoping someone smarter than me can give me the proper explanation (or point me to the documentation) of why the following behavior is as it is.

由于以下code-背后:

Given the following code-behind:

protected string GetMyText(string input)
{
  return "Hello " + HttpUtility.HtmlEncode(input);
}

为什么这部作品

 <asp:Label ID="Label1" runat="server"><%= GetMyText("LabelText") %></asp:Label>

但这并不

<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />

编辑 - 添加

目前有我原来的哑答案的风险downvoted多次,这里的链接,原来的问题,因为一些现在我得到的答案已经覆盖了这个问题。

At the risk of having my original dumb answer downvoted more times, here's the link to the original question, since some of the answers I'm getting now were already covered in that question.

<一个href=\"http://stackoverflow.com/questions/1529944/why-cant-i-set-the-asplabel-text-property-by-calling-a-method-in-the-aspx-file/1529970#1529970\">http://stackoverflow.com/questions/1529944/why-cant-i-set-the-asplabel-text-property-by-calling-a-method-in-the-aspx-file/1529970#1529970

推荐答案

使用&LT;%=%&GT; 等于把响应。写()在你的页面。在这样做时:

Using <%= %> is equal to putting Response.Write("") in your page. When doing this:

<asp:Label ID="Label1" runat="server"><%= GetMyText("LabelText") %></asp:Label>

在ASP.NET处理器渲染时计算控制的话,输出控制的内容和放大器;电话的Response.Write 在那里看到&LT;%=

在这个例子:

<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />

您不能使用的Response.Write()上的文本属性,因为它不返回一个字符串。它的输出写入响应缓冲区并返回void。

You can't use Response.Write("") on a Text attribute because it does not return a string. It writes its output to the response buffer and returns void.

如果你想使用ASP.NET中的标记中的服务器标记语法,你需要使用&LT;%#%&GT; 。标记数据的组合绑定在标签的价值。为了使这项工作,你就那么需要调用的DataBind()在你的页面的加载()它的方法工作。

If you want to use the server tag syntax in ASP.NET markup you need to use <%# %>. This combination of markup data binds the value in the tags. To make this work, you'll then need to call DataBind() in your page's Load() method for it to work.

这篇关于为什么&LT;%=%GT;工作在一种情况下,但不是在另一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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