相当于 PHP 的 Echo 的 ASP.Net 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2977675/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the ASP.Net equivalent to PHP's Echo?
提问by Oded
I want to 'echo' a string separated by delimeters like: sergio|tapia|1999|10am
我想“回显”一个由分隔符分隔的字符串,例如:sergio|tapia|1999|10am
the Body of an HTML page.
HTML 页面的正文。
How can I achieve this? Thank you!
我怎样才能做到这一点?谢谢!
回答by Oded
回答by Chad Levy
You can use Response.Write(str)both in code-behind and on the .ASPX page:
您可以Response.Write(str)在代码隐藏和 .ASPX 页面上使用:
<%
Response.Write(str)
%>
Using Response.Write()in code-behind places the string before the HTML of the page, so it's not always useful.
使用Response.Write()代码隐藏的地方串页面的HTML之前,所以它并不总是有用的。
You can also create a server control somewhere on your ASPX page, such as a label or literal, and set the text or value of that control in code-behind:
您还可以在 ASPX 页面的某处创建服务器控件,例如标签或文字,并在代码隐藏中设置该控件的文本或值:
.ASPX:
.ASPX:
<asp:Label id="lblText" runat="server" />
Code-behind:
代码隐藏:
lblText.Text = "Hello world"
Outputs in HTML:
HTML 格式的输出:
<span id="lblText">Hello World</span>
If you don't want <span>s added, use a literal:
如果您不想<span>添加 s,请使用文字:
<asp:Literal id="litText" runat="server" />
And set the value attribute of the literal instead of the text attribute:
并设置文字的 value 属性而不是 text 属性:
litText.Value = "Hello World"
回答by erfan
In the new Razor syntax, you can just write @variable in your html and its value will be echoed:
在新的 Razor 语法中,您只需在 html 中写入 @variable 并且其值将被回显:
@{
    var name = 'Hiccup';
}
<p>Welcome @name</p>

