相当于 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:19:34  来源:igfitidea点击:

What is the ASP.Net equivalent to PHP's Echo?

phpasp.netecho

提问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

Use Response.Write(string).

使用Response.Write(string).

There are a couple of shortcuts to Response.Writeif you are trying to output on the page:

Response.Write如果您尝试在页面上输出,有几个快捷方式:

<%="sergio|tapia|1999|10am"%>

Or

或者

<%:"sergio|tapia|1999|10am"%> (.NET 4.0)

See herefor the different options.

请参阅此处了解不同的选项。

回答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>