TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET HtmlTextWriter Example

Use HtmlTextWriter from System.Web.UI to generate correctly-formed HTML.
HtmlTextWriter 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. This can lead to more reliable HTML generation methods.
Example. This program introduces a GetDivElements function, which returns a string containing HTML div elements. In the function, we create a new 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.

RenderBeginTag: With RenderBeginTag, you can pass a string of the element name or a constant value in the HtmlTextWriterTag enumeration.

RenderEndTag: At the end of your code that sets up a tag, you must call RenderEndTag. This ends the element HTML.

Compile: To compile, change the project's client profile to .NET 4.0, not the Client Profile. Next, add the reference System.Web.

VB.NET program that uses HtmlTextWriter Imports System.IO Imports System.Web.UI Module Module1 Dim _words As String() = {"dot", "net", "Codex"} 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 Output <div class="c"> <a href="dot"><img src="dot.png" width="200" height="150" /></a> </div><div class="c"> <a href="net"><img src="net.png" width="200" height="150" /></a> </div><div class="c"> <a href="Codex"><img src="Codex.png" width="200" height="150" /></a> </div>
Discussion. Attributes can be added before the RenderBeginTag function is called. We set up class attributes, href attributes, src attributes, and finally width and height attributes on each loop iteration.

Optimization: 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.

Summary. We used the HtmlTextWriter type from the System.Web namespace in the VB.NET language. With this type, we can programmatically generate HTML in-memory, without having to deal with all those syntactic complexities of HTML.

And: We can avoid the brackets and quotes and just write HTML based on where we want the elements to occur in the page structure.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf