In this article I will explain with an example, how to display HTML Formatted Text using Literal control in ASP.Net using C# and VB.Net.
This article will illustrate how to first generate a dynamic HTML Formatted Text String using StringBuilder class and then assign the HTML String to a Literal control and display it on Web Page in ASP.Net using C# and VB.Net.
HTML Markup
The HTML Markup consists of a Literal control.
<asp:Literal ID = "ltTable" runat = "server" />
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Text;
VB.Net
Imports System.Data
Imports System.Text
Display HTML Formatted Text using Literal control in ASP.Net
Inside the Page Load event, first a dynamic DataTable is created with some dummy data.
Then using the StringBuilder class, an HTML String of HTML Table is built and is later assigned to the Literal control’s Text property.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
StringBuilder sb = new StringBuilder();
//Table start.
sb.Append("<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-size: 9pt;font-family:Arial'>");
//Adding HeaderRow.
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>" + column.ColumnName + "</th>");
}
sb.Append("</tr>");
//Adding DataRow.
foreach (DataRow row in dt.Rows)
{
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<td style='width:100px;border: 1px solid #ccc'>" + row[column.ColumnName].ToString() + "</td>");
}
sb.Append("</tr>");
}
//Table end.
sb.Append("</table>");
ltTable.Text = sb.ToString();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable
dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(System.Int32)), New DataColumn("Name", GetType(System.String)), New DataColumn("Country", GetType(System.String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Dim sb As StringBuilder = New StringBuilder()
'Table start.
sb.Append("<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-size: 9pt;font-family:Arial'>")
'Adding HeaderRow.
sb.Append("<tr>")
For Each column As DataColumn In dt.Columns
sb.Append(("<th style='background-color: #B8DBFD;border: 1px solid #ccc'>" _
+ (column.ColumnName + "</th>")))
Next
sb.Append("</tr>")
'Adding DataRow.
For Each row As DataRow In dt.Rows
sb.Append("<tr>")
For Each column As DataColumn In dt.Columns
sb.Append(("<td style='width:100px;border: 1px solid #ccc'>" _
+ (row(column.ColumnName).ToString + "</td>")))
Next
sb.Append("</tr>")
Next
'Table end.
sb.Append("</table>")
ltTable.Text = sb.ToString
End If
End Sub
Screenshot
Downloads