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