htmltextwriter c# vb.net后台输出html,C# HtmlTextWriter Use

You need to write HTML programmatically

using HtmlTextWriter. This allows you to

generate a list of HTML elements, such as

elements. You could use

StringBuilder to create the HTML, but HtmlTextWriter is better.

Overview:This C# tutorial shows HtmlTextWriter and requires

System.Web.UI.

Example

First, the HtmlTextWriter class is found in the System.Web.UI

namespace. If you are not already using ASP.NET, you will need to

reference the System.Web assembly. You also need the System.IO

namespace for StringWriter. HTML is a string and it is easy to

generate HTML using StringBuilder. However, that method is also

prone to many bugs and can result in extremely complex syntax. Here

we use the HtmlTextWriter class to alleviate these problems.

Program that uses HtmlTextWriter [C#]

using System;

using System.IO;

using System.Web.UI;

class Program

{

static string[] _words = { "Sam", "Dot", "Perls" };

static string GetDivElements()

{

// Initialize StringWriter instance.

StringWriter stringWriter = new StringWriter();

// Put HtmlTextWriter in using block because it needs to call Dispose.

using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))

{

// Loop over some strings.

foreach (var word in _words)

{

// Some strings for the attributes.

string classValue = "ClassName";

string urlValue = "http://www.dotnetperls.com/";

string imageValue = "image.jpg";

// The important part:

writer.AddAttribute(HtmlTextWriterAttribute.Class, classValue);

writer.RenderBeginTag(HtmlTextWriterTag.Div); // Begin #1

writer.AddAttribute(HtmlTextWriterAttribute.Href, urlValue);

writer.RenderBeginTag(HtmlTextWriterTag.A); // Begin #2

writer.AddAttribute(HtmlTextWriterAttribute.Src, imageValue);

writer.AddAttribute(HtmlTextWriterAttribute.Width, "60");

writer.AddAttribute(HtmlTextWriterAttribute.Height, "60");

writer.AddAttribute(HtmlTextWriterAttribute.Alt, "");

writer.RenderBeginTag(HtmlTextWriterTag.Img); // Begin #3

writer.RenderEndTag(); // End #3

writer.Write(word);

writer.RenderEndTag(); // End #2

writer.RenderEndTag(); // End #1

}

}

// Return the result.

return stringWriter.ToString();

}

static void Main()

{

// Demonstrate HtmlTextWriter.

Console.WriteLine(GetDivElements());

}

}

Output

Sam

Dot

Perls

Overview. The new StringWriter is required

for the HtmlTextWriter to write to. It is a buffer and everything

that is written is written to this. The using keyword ensures that

the system will Dispose of the objects properly. It is not really

required but may make for more efficient code.

StringWriter

Class

a4c26d1e5885305701be709a3d33442f.png

Foreach loop. Your program will have a

collection of objects it needs to write out, or read from a

database. We simply use a foreach loop for this. The example loops

through a string array and then uses those strings as the HTML text

links.

Foreach Loop

Examples

Make

tags

Here we look at how you need to use "RenderBegin" and "RenderEnd"

methods on the HtmlTextWriter. You may recognize the Push and Pop

methods on queues. Call RenderBeginTag to open a new level in the

HTML with that tag. You must manually close that level of that

markup with the RenderEndTag method call. RenderBeginTag and

RenderEndTag must be in pairs.

Note:Please look at "the

important part" in the code above to follow this description.

Description. It adds an attribute.

Attributes in HTML are similar are used with = and quotes. Add the

attribute with AddAttribute before BeginRenderTag. Look at the

parts of the code that read "HtmlTextWriterAttribute.Class" and

similar. These are enumerations on the specified class. You do not

type the tag names in strings. That would result in typos.

Tags overview. Every tag is started and

then closed later—this is the most important part. Finally, the

Write method is an instance method that writes text in between the

tags in HTML.

Tabstring

parameter

In the constructor to the HtmlTextWriter, use an empty string as

the second parameter. This will eliminate a lot of whitespace, and

I was able to reduce the size of my pages by a small percentage

this way.

Empty String

Examples

Fragment that uses string.Empty [C#]

using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter,

string.Empty))

{

// etc.

}

a4c26d1e5885305701be709a3d33442f.png

XmlWriter

Using XmlWriter is very similar to the code shown here, but the

output is XML. This is useful for interoperability, whereas

HtmlTextWriter is mainly useful for web pages in ASP.NET.

XmlWriter

Tutorial

Summary

a4c26d1e5885305701be709a3d33442f.png

Here we saw how you can use the HtmlTextWriter class in the

System.Web namespace using the C# language. Use HtmlTextWriter to

write HTML instead of StringBuilder alone for the cleanest and

object-oriented code. I am not aware of any performance issues, and

the markup is always nicely indented.

HTML Articles

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值