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.

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.

Conclusion:

Any advice or correction for this class will be highly appreciated.

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.

Share

About the Author

Mohammad Ashraful Alam is a Software Engineer, who is dedicated to Microsoft .NET based development. This Bangladeshi national is involved with project management and development of several US based software projects from his country. Already he has managed and developed 15 software projects, which are being used by several users of different countries, such as USA, Canada, Australia, and Bangladesh. While developing and managing a team, he contains and maintains a set of well defined engineering practices developed by him and other online developer community. Beside software development, he has also written several technical articles and research papers published by IEEE Computer Society and many other worlds recognized publishers.

Before becoming engaged with software development, he was involved with Bengali literature and several Bengali news papers as freelance journalist and published around 150 articles, essays and short stories.

Due to his willingness to give effort to improve and share better software development practices, Ashraf has awarded as “Most Valuable Professional” (MVP) in ASP.NET category by Microsoft for multiple times, since 2007.

When not engaged with technical stuffs, he likes to pass time with his friends, and family members, listens music or watches TV.

Comments and Discussions

1. You could use a XmlDocument object to build the HTMLs rather using a StringBuilder, that could make the code XHTML complaint and more managable + readable. It also could help you removing the ugly string concatanation that you did.
2. You could put some more useful methods in it to make it actually useful and generic for the usual users. Currently it doesn't provides adequate methods so that somebody else might find it useful.
3. On the contrary of my earlier point no 1, I would say the best thing you should do is define APIs like, HtmlWriter, that can take StreamWriter as an input and can populate the DOM -much like what .net framework provided API set- "XmlWriter.. etc" does. if you are writing this API for .net user this must be the way so that the user can find it consistent and useful.

Finally, it's nice to avoid human as a namespace-this definitely is not a good excuse of code's "readability".

The Web and HTML controls can be used to generate HTML code without creating the declarative controls on a page. They are just objects that produce HTML code. Here is an example in C++/CLI in a Console program:

The Web and HTML controls can be used to generate HTML code without creating the declarative controls on a page. They are just objects that produce HTML code. Here is an example in C++/CLI in a Console program:

using namespace System;
using namespace System::Web::UI;
using namespace System::Web::UI::WebControls;
using namespace System::Web::UI::HtmlControls;

using System::Web::UI::HtmlControls::HtmlAnchor;
using System::Web::UI::WebControls::TextBox;
using System::Web::UI::HtmlTextWriter;

Well George, let's say we have to send some data retrieved from database thru mail. Then what's the simpler solution? To make a fake textbox control to format the data in HTML? I don't believe your solution is a good sample regarding this situation.

Next, 'Code Readability' is definitely a good thing that should reside on your codes. I am sure you will agree that regarding the formatted html text and lay-outs of ALL text content in the target HTML table 'HTML Builder Utility Class' will provide you more abstraction and better look ( and thus 'Readability') while coding.

Actually, I just wrote the code to prove you can use HTML and Web controls to produce HTML. It was not an exercise to compete with what you have presented. It is possible to use the controls in a wrapped class similar to yours without using a third party solution. Where I work, I am not allowed to use any unapproved "Open Source" software. (The approval process takes along time.) So, I have to depend on my own solutions most of the time.

Also, there is an ATL solution that looks a lot like your code in native C++ that I have been using for a few years.

Thanks for the codes. This code is specifically useful when you are concerned the HTML rendering of any specific controls or special HTML formatting, where this method will provide necessary piece of HTML.

However having several controls which contains data to be rendered in HTML as well as formatting the layout of the overall page are a bit complex and time consuming. When our application requirements to render data from data (which may contain in UI control or data-source), our "HTML Builder" class will save your time.

Your solution clearly simplifies the process. I would say add overloads for each method that include the data for the element and the css class name. Also a method to add a reference to a CSS file would be nice. I like it, much better then creating a bunch of new objects...

Your solution clearly simplifies the process. I would say add overloads for each method that include the data for the element and the css class name. Also a method to add a reference to a CSS file would be nice. I like it, much better then creating a bunch of new objects...

Thank you very much for your creative ideas. I will add some more features along with your ideas in near future, so all developers find it more useful.

One thing I found valuable was to return the generated object because I found that I needed to nest controls. Obviously, you'd then need to add additional overloads that let you pass the container for the control to generate.

You can keep building on this concept and create methods that auto generate scrollable regions, collapsable regions, and more.

One thing I found valuable was to return the generated object because I found that I needed to nest controls. Obviously, you'd then need to add additional overloads that let you pass the container for the control to generate.

You can keep building on this concept and create methods that auto generate scrollable regions, collapsable regions, and more.