Introduction
When you develop a web based application, you need some texts to be converted to HTML tags for a convenient format. This is required especially when you have to send e-mail or show text in 'Print' page. Definitely there are lots of utility classes and tools are available in web. But fortunately some of my application requirements regarding HTML text were very simple and unfortunately I didn't find anything in the web to get a simple HTML builder utility to format my simple texts. However, as usual I decided to write an utility class, which is very simple to use.
Using the class
Let's look at the sample codes that use our Simplified HTML Builder Class.
public static void UseThis()
{
Ashraf.HTMLUtil util = new Ashraf.HTMLUtil();
util.AppendHeader("Header");
util.AppendSubHeader("Sub Header");
util.AppendPara("Hello this is a para");
util.AppendPara("Hello this is the second para");
util.AppendBlankRow();
util.FormatToRowInColonSeparatedText("Label 1","Value 1", false);
util.FormatToRowInColonSeparatedText("Label 2","Value 2", false);
util.AppendBlankRow();
util.AppendPara("Hello this is footer para");
util.AppendSubHeader("Sub Footer");
util.AppendHeader("Footer");
System.Web.HttpContext.Current.Response.Write(util.FormattedHTMLText);
}
At first you have to create an instance of "HTMLUtil" class. After then using different methods you can add your contents into this instance. Internally a System.Text.StringBuilder class instance is the container of all of the contents in the corresponding format. The utility class methods wrap all of contents to an html table so that the contents can easily be placed anywhere. Using the "HTMLUtil" instance you can add header, sub-header, normal texts, blank rows etc.
util.AppendHeader("Header");
util.AppendSubHeader("Sub Header");
util.AppendPara("Hello this is a para");
util.AppendBlankRow();
However as you see in the code, there is a method named "FormatToRowInColonSeparatedText". This method appends colon separated row in the System.Text.StringBuilder container along with the passed parameters as caption and value. This is useful when you want to format html for database table fields.
util.FormatToRowInColonSeparatedText("Label 1","Value 1", false);
When you finish your texts to be built in desired formatting, you are ready to use that. You will get all the texts in proper formatted thru the "FormattedHTMLText" property of the class.
System.Web.HttpContext.Current.Response.Write(util.FormattedHTMLText);
However, you can define the size of the fonts of header, sub-header, normal text and the font face for the builder class by the following properties.
public int HeaderTextSize{get;set;}
public int SubHeaderTextSize{get;set;}
public int NormalTextSize{get;set;}
public string FontFace{get;set;}
Conclusion:
Any advice or correction for this class will be highly appreciated.