Displaying Formatted Text

by | 11 Feb 2017

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

Submit a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Other Posts

National Pride vs. People’s Lives

More lives could have been saved but the Bangladeshi government reportedly refused to take assistance from some friendly countries in rescuing garment workers from collapsed “Rana Plaza” because of national pride. I am not sure where is the pride coming from. Other...

Social media

I have some friends who don’t like Facebook or twitter (may be they even hate it). I told them to join several times but they avoided by giving many excuses. I think social media makes communication easier. I only have their phone numbers but sometimes I don’t want to...

Voted

Last Tuesday was Election Day. There was not enough publicity about the candidates, so I didn't know anyone or their agenda. You wouldn't believe; I voted for the names that sounded better. The vote started at 7:00AM and I voted at 5:00PM. Yet, I was the 25th voter....

Graduation Party

On Saturday 02 July 2022, we celebrated important milestones of our children. We had four graduates this year in our family - one graduated from kindergarten, two from elementary schools, and the oldest one among them from middle school. Kids were free to spend as...

My worst experience with doctors

It was December 2015, my second son was playing with colored cotton balls and I thought I saw him putting one small cotton ball in one of his nostrils (he was 1 year+ at that time). We saw something red in his nose but we weren’t 100% sure because the inside of a nose...

Identity Theft

I guess finally my name is added to the list of 41 million Americans who are victims of stolen identities. Someone opened credit card twice in Kohl's (https://www.kohls.com/) using my credentials. First time, I was able to cancel the card before it was used but second...

Neighborhood Nuisance

It was about 9:30pm on Saturday 17 December 2022, someone knocked our door hard – first I thought it was an Amazon delivery guy but didn’t see anyone or nothing was left on our doorstep. We checked the footage from our security camera and saw someone was running away...