Dynamically displaying formatted text is a pain. A label control can’t keep the format. People suggest to use multiline texbox but that doesn’t look good in a web page for announcement. Some suggests to use RichTextBox but that requires a great deal of coding. I always look for a simple solution, so I created a HtmlGenericControl (pre) on the fly and added it to a panel control. Hola, the format was protected. You can even add html control to a label too; I tested and it worked. Afterwards, you can use necessary style in the pre tag for your need.
//need to use the following library/namespace
Using System.Web.UI.HtmlControls;
//code behind
HtmlGenericControl hgcPre = new HtmlGenericControl(“pre”);
hgcPre.InnerText = “your formatted text”; //in my case data comes from a table
pnlFormatted.Controls.Add(hgcPre);
//style
Pre
{
font-family: Tahoma, Verdana, Helvetica, Sans-Serif;
}

0 Comments