C# 如何在 ASP.NET 中以编程方式创建 <h1> 标记?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11191951/
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
how do you create an <h1> tag programmatically in ASP.NET?
提问by Mike Corcoran
I'm trying to generate some html programmatically in my code behind for a user control I'm designing.
我正在尝试在我正在设计的用户控件背后的代码中以编程方式生成一些 html。
I've been looking around, but can't seem to figure out how to dynamically generate some h1 tags for content I'll be displaying.
我一直在环顾四周,但似乎无法弄清楚如何为我将要显示的内容动态生成一些 h1 标签。
Is it just a Label with a special property set?
它只是一个带有特殊属性集的标签吗?
采纳答案by Pein
var h1 = new HtmlGenericControl("h1");
h1.InnerHtml = "header content";
回答by Kapil Khandelwal
You can use label or literal controls as shown below:
您可以使用标签或文字控件,如下所示:
Label1.Text = "<h1>HI All</h1>";
Or
或者
string heading = "HI All";
Label1.Text = string.Format("<h1>{0}</h1>", heading);
回答by rs.
Use Literal control, Literal1.Text = "<h1>" + texttodisplay + "</h1>";
使用文字控制, Literal1.Text = "<h1>" + texttodisplay + "</h1>";
回答by Abdullah Musani
You can do this easily by
你可以很容易地做到这一点
literall.Text ="<h1>ABCD</h1>";
回答by Tim
You could also use:
您还可以使用:
<h1 runat="server" />
回答by nik0lias
ASP literals may help you.
ASP 文字可能对您有所帮助。
http://ganeshmohan.wordpress.com/aspnet-and-c-generate-html-controls-dynamically/
http://ganeshmohan.wordpress.com/aspnet-and-c-generate-html-controls-dynamically/
回答by Rasel
Let, you have a server control like this,
让,你有一个这样的服务器控件,
<div class="cls" runat="server" id="MyServerControlDiv"></div>
using HtmlGenericControl
使用 HtmlGenericControl
var h1 = new HtmlGenericControl("h1");
h1.InnerHtml = "header content";
MyServerControlDiv.Controls.Add(h1);
using LiteralControl
使用 LiteralControl
MyServerControlDiv.Controls.Add(new LiteralControl("<h1>"));
MyServerControlDiv.Controls.Add(new LiteralControl("header content"));
MyServerControlDiv.Controls.Add(new LiteralControl("</h1>"));
回答by Licht
Personally I prefer to make a class for it.
我个人更喜欢为它创建一个类。
public class H1: HtmlGenericElement
{
public H1(): base("h1") { }
}
Or maybe.
或者可能。
public class H: HtmlGenericControl
{
public H(int size): base("h" + size) { }
}
回答by noobmaster007
Let us assume that you have an asp placeholder in your design:
让我们假设您的设计中有一个 asp 占位符:
PlaceHolder placeHolder1 = new PlaceHolder();
string yourText = "Header Text";
Label myLabel = new Label();
myLabel.Text = "<h1>" + yourText + "</h1>";
PlaceHolder1.Controls.Add(myLabel);
OR
或者
PlaceHolder1.Controls.Add(new LiteralControl ("<h1>")) // and likewise
for </h1>.
回答by Colin Gardner
Using a generic HTML control or a literal control is fine just so long as you don't want to insert any child controls. If you do need to append a child control such as a label for example, then you will want to create you header as a Web Control like follows:
只要您不想插入任何子控件,就可以使用通用 HTML 控件或文字控件。例如,如果您确实需要附加子控件(例如标签),那么您需要将标题创建为 Web 控件,如下所示:
WebControl H2Header = new WebControl(HtmlTextWriterTag.H1);
And then you can append a child control like this:
然后你可以像这样附加一个子控件:
Label labHeader = new Label() { Text = "Some Header Text" };
H2Header.Controls.Add(labHeader);

