I am developing an ASP.NET web application at work, and I keep running into the same issue:
我正在开发一个ASP。NET web应用程序在工作中,我一直遇到同样的问题:
Sometimes I want to write HTML to the page from the code-behind. For example, I have a page, Editor.aspx, with output which varies depending on the value of a GET variable, "view." In other words, "Editor.aspx?view=apples" outputs different HTML than "Editor.aspx?view=oranges".
有时,我想从代码背后将HTML写入页面。例如,我有一个页面,编辑器。aspx,输出根据GET变量的值而变化,“view”。换句话说,“Editor.aspx ?“view=apple”输出的HTML与“Editor.aspx?view=桔子”不一样。
I currently output this HTML with StringBuilder. For example, if I wanted to create a list, I might use the following syntax:
我现在用StringBuilder输出这个HTML。例如,如果我想创建一个列表,我可以使用以下语法:
myStringBuilder.AppendLine("<ul id=\"editor-menu\">");
myStringBuilder.AppendLine("<li>List Item 1</li>");
myStringBuilder.AppendLine("</ul>");
The problem is that I would rather use ASP.NET's List control for this purpose, because several lines of StringBuilder syntax hamper my code's readability. However, ASP.NET controls tend to output convoluted HTML, with unique ID's, inline styles, and the occasional block of inline JavaScript.
问题是我宁愿使用ASP。NET的列表控件为此目的,因为有几行StringBuilder语法妨碍了我的代码的可读性。然而,ASP。NET控件倾向于输出复杂的HTML,具有独特的ID、内联样式和偶尔的内联JavaScript块。
My question is, is there an ASP.NET control which merely represents a generic HTML tag? In other words, is there a control like the following example:
我的问题是,是否存在ASP。NET控件,它仅仅表示一个通用的HTML标记?换句话说,是否存在如下示例所示的控件:
HTMLTagControl list = new HTMLTagControl("ul");
list.ID = "editor-menu";
HTMLTagControl listItem = new HTMLTagControl("li");
listItem.Text = "List Item 1";
list.AppendChild(listItem);
I know there are wrappers and the such, but instead of taming ASP.NET complexity, I would rather start with simplicity from the get-go.
我知道有包装和类似的东西,但不是驯服ASP。纯粹的复杂性,我宁愿从简单开始。
3 个解决方案
#1
8
is there an ASP.NET control which merely represents a generic HTML tag?
有一个ASP。NET控件,它仅仅表示一个通用的HTML标记?
Yes, it's called the HtmlGenericControl :)
是的,它叫做HtmlGenericControl:)
As far as exactly what you want no, but you can get around it easily:
至于你到底想要什么,没有,但你可以很容易地绕过它:
HtmlGenericControl list = new HtmlGenericControl("ul");
list.ID = "editor-menu";
HtmlGenericControl listItem = new HtmlGenericControl("li");
listItem.InnerText = "List Item 1";
list.Controls.Add(listItem);
If you really need to get down to bare metal then you should use the HtmlTextWriter class instead of a StringBuilder as it is more custom tailored to pumping out raw HTML.
如果您真的需要使用裸金属,那么您应该使用HtmlTextWriter类,而不是StringBuilder,因为它是为输出原始HTML而定制的。
#2
2
If you want to just assign the results of your stringbuilder to a blank control, you can use an <asp:Literal />
control for this.
如果要将stringbuilder的结果分配给空白控件,可以使用
#3
1
LiteralControl has constractor that you can pass your html... i think it is better.
文字控制有限制,你可以通过你的html…我认为这样更好。
new LiteralControl(sb.ToString());
新LiteralControl(sb.ToString());
#1
8
is there an ASP.NET control which merely represents a generic HTML tag?
有一个ASP。NET控件,它仅仅表示一个通用的HTML标记?
Yes, it's called the HtmlGenericControl :)
是的,它叫做HtmlGenericControl:)
As far as exactly what you want no, but you can get around it easily:
至于你到底想要什么,没有,但你可以很容易地绕过它:
HtmlGenericControl list = new HtmlGenericControl("ul");
list.ID = "editor-menu";
HtmlGenericControl listItem = new HtmlGenericControl("li");
listItem.InnerText = "List Item 1";
list.Controls.Add(listItem);
If you really need to get down to bare metal then you should use the HtmlTextWriter class instead of a StringBuilder as it is more custom tailored to pumping out raw HTML.
如果您真的需要使用裸金属,那么您应该使用HtmlTextWriter类,而不是StringBuilder,因为它是为输出原始HTML而定制的。
#2
2
If you want to just assign the results of your stringbuilder to a blank control, you can use an <asp:Literal />
control for this.
如果要将stringbuilder的结果分配给空白控件,可以使用
#3
1
LiteralControl has constractor that you can pass your html... i think it is better.
文字控制有限制,你可以通过你的html…我认为这样更好。
new LiteralControl(sb.ToString());
新LiteralControl(sb.ToString());