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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/969414/
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-06 04:30:26  来源:igfitidea点击:

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

c#asp.net

提问by Magnetic_dud

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.

我需要根据字符串变量更改我正在编码的 asp.net 页面区域中的一些 html。我正在考虑创建一个标签,然后更改其上的文本。

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 ?

我能怎么办?使用 response.write?如果我使用 response.write,我添加的代码将位于 html 源代码的开头,我如何告诉他将它添加到特定的代码中?

Thank you

谢谢

采纳答案by TheVillageIdiot

why don't you give LiteralControla try?

为什么不试试LiteralControl呢?

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

回答by Mehrdad Afshari

If you really don't want to use any server controls, you should put the Response.Writein 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>

回答by tekBlues

Use a literal control and write your html like this:

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

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

回答by Philippe Leybaert

You should really use the LiteralASP.NET control for that.

您真的应该为此使用LiteralASP.NET 控件。

回答by cllpse

ASPX file:

ASPX 文件:

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

ASPX.CS file:

ASPX.CS 文件:

ltNotify.Text = "Alert!";

回答by Meetu Choudhary

You can go with the literal control of ASP.net or you can use panels or the purpose.

您可以使用 ASP.net 的文字控件,也可以使用面板或用途。

回答by Marc Stober

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", e.g.:

如果你想要比 Label 或其他 ASP.NET 特定的服务器控件更轻的东西,你可以只使用标准的 HTML DIV 或 SPAN 和 runat="server",例如:

Markup:

标记:

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

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

Code:

代码:

FooSpan.Text = "Foo";

FooSpan.Text = "Foo";

回答by ShaileshDev

You can also use pageMethods in asp.net. So that you can call javascript functions from asp.net functions. E.g.

您也可以在 asp.net 中使用 pageMethods。这样您就可以从 asp.net 函数调用 javascript 函数。例如

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

这里上面的代码是写在 .aspx.cs 页面中的。数据库是另一个类。在 showResult() 函数中,我调用了 javascript 的 link() 函数。结果以表格形式显示。