Show line breakes in Asp.net Label


To show line breakes in a server side asp.net label, you can either use a HTML formatted text or a plain text.

HTML formatted text contains “<br>” tags so while rendring the control .net framework handles the task to show the proper html formatted text into a Label.

Example:
lblDescription.Text=”name <br>description”;
The above code will display a line gap between name and description.
But in case if user inputted data using a non-html enabled textbox then it is a point of problem.

While displaying data, inputted by simple multiline textbox, line breaks looses its state and when the data displayed on a  label it looks like a simple constant text line.  Interestingly if you try to show the same data back in a textbox it will look nice with proper line breaks. But we can’t use textbox every where because of designing requirements.

To keep the line breaks you must convert the html new line special indicator to HTML break “<br>” tag.

Solution:
lblDescription.Text = txtInput.Text.ToString().Replace(Environment.NewLine, “<br />”);

The above code replaces the “NewLine” indications to “<br>” at run time, and displays the data same as input formatting.

For demo source code visit
http://dotnetcoderoom.blogspot.com/2008/09/show-line-breakes-in-aspnet-label.html

Advertisements

About dotnetcoderoom

Working as an Onsite coordinator in USA. Handling UAT, CR (Change Requests), Product Support/Development, Build Management. Supervise and assist in live product implementation (at client side) and training. Providing technical direction for the development, design, and system integration. Closely monitoring and working with offshore team for Enhancements, Bug Fixing, Enforce coding standards, perform code reviews and mentors junior developers. Proven track of timely delivery of enterprise level web based applications. Skilled in a variety of software languages, tools, and methodologies, with a special expertise in Microsoft .NET technologies. MCTS .Net 4.0 Web applications MCTS .Net 2.0 Web applications GNIIT, MCA, B.Com Specialties 8+ Years of experience in .NET Technologies. Expertise in .NET, ASP.NET, MVC, C#, VB.NET, WCF, Web Services, MS SQL, JQuery. Excellent R&D and troubleshooting skills. Extensive knowledge in E-Commerce and Healthcare domain. View all posts by dotnetcoderoom

One response to “Show line breakes in Asp.net Label

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: