简体   繁体   中英

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. 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? 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 ?

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:

<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:

Markup:

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

Code:

FooSpan.Text = "Foo";

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

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

ASPX file:

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

ASPX.CS file:

ltNotify.Text = "Alert!";

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

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

You can also use pageMethods in asp.net. So that you can call javascript functions from asp.net functions. 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. Database is another class. In showResult() function I've called javascript's link() function. Result is displayed in the form of table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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