简体   繁体   English

ASP.NET - 如何在页面中编写一些html? 使用Response.Write?

[英]ASP.NET - How to write some html in the page? With Response.Write?

I need that some html in the area in the asp.net page that i am coding, is changed according to a string variable. 我需要在我编码的asp.net页面区域中的一些html,根据字符串变量进行更改。 I was thinking about creating a label, and then change the text on it. 我正在考虑创建一个标签,然后更改它上面的文本。

But the string variable contains something like: 但字符串变量包含如下内容:

<h2><p>Notify:</p> alert</h2>

So, I don't feel that give this to a label text is a good idea 所以,我不认为给标签文本这个是一个好主意

How i can do? 我该怎么办? Using response.write? 使用response.write? If I use response.write, my added code will be at the beginning of the html source, how i can tell him to add it in a specific ? 如果我使用response.write,我添加的代码将在html源代码的开头,我怎么能告诉他将它添加到特定的?

Thank you 谢谢

If you really don't want to use any server controls, you should put the Response.Write in the place you want the string to be written: 如果您确实不想使用任何服务器控件,则应将Response.Write放在要写入字符串的位置:

<body>
<% Response.Write(stringVariable); %>
</body>

A shorthand for this syntax is: 此语法的简写如下:

<body>
<%= stringVariable %>
</body>

你为什么不试试LiteralControl

 myLitCtrl.Text="<h2><p>Notify:</p> Alert</h2>";

If you want something lighter than a Label or other ASP.NET-specific server control you can just use a standard HTML DIV or SPAN and with runat="server", eg: 如果您想要比Label或其他特定于ASP.NET的服务器控件更轻的东西,您可以使用标准HTML DIV或SPAN以及runat =“server”,例如:

Markup: 标记:

<span runat="server" id="FooSpan"></span>

Code: 码:

FooSpan.Text = "Foo";

使用文字控件并像这样编写你的html:

literal1.text = "<h2><p>Notify:</p> alert</h2>";

ASPX file: ASPX文件:

<h2><p>Notify:</p> <asp:Literal runat="server" ID="ltNotify" /></h2>

ASPX.CS file: ASPX.CS文件:

ltNotify.Text = "Alert!";

你应该真的使用Literal ASP.NET控件。

您可以使用ASP.net的字面控制,也可以使用面板或目的。

You can also use pageMethods in asp.net. 您也可以在asp.net中使用pageMethods。 So that you can call javascript functions from asp.net functions. 这样你就可以从asp.net函数中调用javascript函数。 Eg 例如

  [WebMethod] public static string showTxtbox(string name) { return showResult(name); } public static string showResult(string name) { Database databaseObj = new Database(); DataTable dtObj = databaseObj.getMatches(name); string result = "<table border='1' cellspacing='2' cellpadding='2' >" + "<tr>" + "<td><b>Name</b></td>" + "<td><b>Company Name</b></td>" + "<td><b>Phone</b></td>"+ "</tr>"; for (int i = 0; i < dtObj.Rows.Count; i++) { result += "<tr> <td><a href=\\"javascript:link('" + dtObj.Rows[i][0].ToString().Trim() + "','" + dtObj.Rows[i][1].ToString().Trim() +"','"+dtObj.Rows[i][2]+ "');\\">" + Convert.ToString(dtObj.Rows[i]["name"]) + "</td>" + "<td>" + Convert.ToString(dtObj.Rows[i]["customerCompany"]) + "</td>" + "<td>"+Convert.ToString(dtObj.Rows[i]["Phone"])+"</td>"+ "</tr>"; } result += "</table>"; return result; } 

Here above code is written in .aspx.cs page. 上面的代码是在.aspx.cs页面中编写的。 Database is another class. 数据库是另一个类。 In showResult() function I've called javascript's link() function. 在showResult()函数中,我调用了javascript的link()函数。 Result is displayed in the form of table. 结果以表格的形式显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM