Home
VB.NET
HtmlTextWriter Example
Updated May 23, 2025
Dot Net Perls
HtmlTextWriter. This writes HTML markup. With it we do not have to deal directly with the syntax. We do this by adding elements with RenderBeginTag and RenderEndTag.
Type notes. HtmlTextWriter can lead to more reliable HTML generation methods. There may be less syntax errors and problems with escaped quotes.
This program introduces GetDivElements, which returns a string of HTML div elements. In the function, we create a StringWriter, which serves as the backing store of the HtmlTextWriter.
Next We create the HtmlTextWriter as pass it the StringWriter instance in its New function.
Tip With RenderBeginTag, you can pass a string of the element name or a constant value in the HtmlTextWriterTag enumeration.
Tip 2 At the end of your code that sets up a tag, you must call RenderEndTag. This ends the element HTML.
Imports System.IO Imports System.Web.UI Module Module1 Dim _words As String() = {"dot", "net", "perls"} Function GetDivElements() As String Using sw As StringWriter = New StringWriter Using ht As HtmlTextWriter = New HtmlTextWriter(sw) For Each word As String In _words ' Create the div. ht.AddAttribute(HtmlTextWriterAttribute.Class, "c") ht.RenderBeginTag(HtmlTextWriterTag.Div) ' Create the a. ht.AddAttribute(HtmlTextWriterAttribute.Href, word) ht.RenderBeginTag(HtmlTextWriterTag.A) ' Create the img. ht.AddAttribute(HtmlTextWriterAttribute.Src, word + ".png") ht.AddAttribute(HtmlTextWriterAttribute.Width, "200") ht.AddAttribute(HtmlTextWriterAttribute.Height, "150") ht.RenderBeginTag(HtmlTextWriterTag.Img) ' End all the tags. ht.RenderEndTag() ht.RenderEndTag() ht.RenderEndTag() Next End Using Return sw.ToString() End Using End Function Sub Main() Console.WriteLine(GetDivElements()) End Sub End Module
<div class=&quot;c&quot;> <a href=&quot;dot&quot;><img src=&quot;dot.png&quot; width=&quot;200&quot; height=&quot;150&quot; /></a> </div><div class=&quot;c&quot;> <a href=&quot;net&quot;><img src=&quot;net.png&quot; width=&quot;200&quot; height=&quot;150&quot; /></a> </div><div class=&quot;c&quot;> <a href=&quot;perls&quot;><img src=&quot;perls.png&quot; width=&quot;200&quot; height=&quot;150&quot; /></a> </div>
Attributes can be added before the RenderBeginTag function is called. We set up class attributes, href attributes, src attributes, and width and height attributes on each loop iteration.
Detail When using HtmlTextWriter, it is best to pass it a buffer you are already using instead of a StringWriter.
Tip The buffer must be derived from TextWriter. Try passing a buffer to HtmlTextWriter and see if the program compiles.
Info We can avoid the brackets and quotes and just write HTML based on where we want the elements to occur in the page structure.
Summary. We used the HtmlTextWriter type from the System.Web namespace. With this type, we can generate HTML in-memory, without having to deal with all those syntactic complexities of HTML.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on May 23, 2025 (edit).
Home
Changes
© 2007-2025 Sam Allen