Click here to Skip to main content
15,886,110 members
Articles / Web Development / HTML
Article

Utility for rendering HTMLTable control containing ASP .Net form elements into string

Rate me:
Please Sign up or sign in to vote.
2.06/5 (9 votes)
24 Sep 20052 min read 35K   231   16  
This utility generates HTML / VB .Net code for converting HTMLTable control containing ASP .Net form elements into html

Introduction

Many websites have some kind of contact form as shown in the following figure. User fills details and presses the "Send" button. This form takes input from the textbox, checkbox, radiobutton etc, builds string containing html and sends information as HTML email. If the form is expecting many values from user, the developer has to write down big chunk of HTML code in the backend. If the developer has to build more than one such forms, it is really cumbersome. To reduce the precious time of developer, this utility module is developed.

Image 1

Problem in Direct Rendering of Control

We can use directly Control.Render() method to render html in HTMLTextWriter but with the condition that there should not be any webform controls like TextBox, Label etc. If still we are using this method, it will give error because each server control must be rendered within "form" tag which is having "runat=server" attribute.

Utility Module

VB
Public Function GetHTML(ByVal objControl As Control) As String<br>    Dim sb As New System.Text.StringBuilder<br>    GenerateHTML(objControl, sb)<br>    Return sb.ToString<br>End Function

This function takes as argument the control object of which we need to get HTML. It will call private method GenerateHTML() which is responsible for generating HTML for the control.

VB
Private Sub GenerateHTML(ByVal objControl As Control, ByVal sb As System.Text.StringBuilder)<br>    Dim str As New System.Text.StringBuilder<br>    Dim sw As New System.IO.StringWriter(str)<br>    Dim hw As New System.Web.UI.HtmlTextWriter(sw)<br>    If TypeOf (objControl) Is HtmlTable Then<br>        sb.Append("<table ")<br>        CType(objControl, HtmlTable).Attributes.Render(hw)<br>        sb.Append(str.ToString & " >")<br>        Dim objControl1 As Control<br>        For Each objControl1 In objControl.Controls<br>            GenerateHTML(objControl1, sb)<br>        Next<br>        sb.Append("</table>")<br>    ElseIf TypeOf (objControl) Is HtmlTableRow Then<br>        sb.Append("<tr ")<br>        CType(objControl, HtmlTableRow).Attributes.Render(hw)<br>        sb.Append(str.ToString & " >")<br>        Dim objControl1 As Control<br>        For Each objControl1 In objControl.Controls<br>            GenerateHTML(objControl1, sb)<br>        Next<br>        sb.Append("</tr>")<br>    ElseIf TypeOf (objControl) Is HtmlTableCell Then<br>        sb.Append("<td ")<br>        CType(objControl, HtmlTableCell).Attributes.Render(hw)<br>        sb.Append(str.ToString & " >")<br>        Dim objControl1 As Control<br>        For Each objControl1 In objControl.Controls<br>            GenerateHTML(objControl1, sb)<br>        Next<br>        sb.Append("</td>")<br>    ElseIf TypeOf (objControl) Is LiteralControl Then<br>        sb.Append(CType(objControl, LiteralControl).Text)<br>    ElseIf TypeOf (objControl) Is TextBox Then<br>        sb.Append(CType(objControl, TextBox).Text)<br>    ElseIf TypeOf (objControl) Is Label Then<br>        sb.Append(CType(objControl, Label).Text)<br>    ElseIf TypeOf (objControl) Is CheckBox Then<br>        Dim chk As CheckBox = CType(objControl, CheckBox)<br>        sb.Append("<input type='checkbox'" & IIf(chk.Checked, "Checked ", " >") & chk.Text)<br>    ElseIf TypeOf (objControl) Is RadioButton Then<br>        Dim rad As RadioButton = CType(objControl, RadioButton)<br>        sb.Append("<input type='radio'" & IIf(rad.Checked, "Checked >", " >") & rad.Text)<br>    End If<br>End Sub<br>

This function uses recursion to get each element from control hierarchy. It renders HTML code for the control. Currently it supports, HTMLTable, TextBox, CheckBox, RadioButton, Label, and LiteralControl. As this function uses recursion, the processing may be slower than hardcoding the HTML string. So I wrote other function GetCode() which is responsible for emitting source code for building HTML string. This code you can put in a function and get HTML string of the control. The output of both methods are shown in figure.

Image 2

Conclusion

If webform is not used very frequently, developer should use, GetHTML() function otherwise they should use use and throw function GetCode(), generate the code, put the code in file and remove this function from solution.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
For more info visit http://www.vishalon.net

Comments and Discussions

 
-- There are no messages in this forum --