Wednesday, April 20, 2011

Convert ASP Controls into HTML Equivalent

Best and Easy Way to Convert ASP Controls into HTML Equivalent: (VB.NET)

Introduction:
Sometimes we need to draw some tabular structure at server side depending on various values retrieved from database i.e. everything should build dynamically. Something like report on various criterion. It’s very hectic and long process when we write all the desired code, structure of report in HTML. In this tutorial I’ll give you a brief but very useful way to design such reports using solo ASP tables or ASP TABLE inside HTML table, where everything will be done at server side.
Example Description:
We will start this tutorial with a simple example; let us assume that we have to generate a report which should printable and some of its portion (say header data, some information) on each page of report.
Here is aspx page which we are going to use to generate example report.
ReportToPrint.aspx


As you can see in above aspx page that I have taken in this example have One Image Button control with ID “btprint”, a ASP table with ID “header” is used to display some information mostly Static. Now 3rd control of the page which is basically is going to contain heart of design in this tutorial.
I have also used a javascript function “fprintf()” to hide unwanted information during print function call, you can use your own as per need.

Now it’s time to let you review VB code (page) to generate dynamic report using HTML tags, ASP Controls which will be converted later on to HTML equivalent:
First look at code below once:
ReportToPrint.aspx.vb












What the Above Code Exactly Doing?
I hope you all read above code carefully, and probably understood what it’s doing. Still let’s have a quick recap:
1. On Page Load I Called Two Functions i.e. TopHeaderRows(), Data_To_Print() TopHeaderRows() function creates a specific header rows which will be constant (probably) for all reports under category.
Data_To_Print() this function is user to generate report based on conditional values passed for report, For example “Allotment Letter for All New Employees In Last Month” this report may need date, department etc. I called this in page_load section, you call it anywhere as per need.
2. TopHeaderRows() function creates a asp table with various styles on it at server side, which will be used in report at various paces with HTML representation.
3. createHeader(),fillData(),ControlToHtml() functions used inside Data_To_Print to generate desired Output Report and it’s formatting as per need.
4. ControlToHtml() is specific function which can be use for conversion of any asp tag to it’s equivalent HTML, just by passing it’s id.
Someone may ask at this point why I should use ASP controls when at the end all things will delivered in HTML format as above function in converting them into HTML when I can code in HTML well.
First answer for this question is “It saves lots of time spent in designing and controlling alignment using HTML”.
Second one can Use various rich controls from ASP in their report which not available in HTML.
So Get Free from Overhead of Design Reports using only HTML tags, Use ASP Controls freely as here we have a function to convert it in HTML equivalent. You can mix both HTML TAGS and ASP CONTROLS as per your need.

No comments:

Post a Comment