Quantcast
750×100
Read WebProNews
With Friends!

Better String Control In ASP.NET

Get the WebProNews Newsletter:

Whenever I had to add a string to a web controls control collection I’ve always used the Literal control like this:

Literal str = new Literal();

str.Text = “hello world”;

Page.Controls.Add(str);

I have always thought a Literal was too cumbersome to use for a simple thing like adding a string to a controls control collection. Then, the other day, I found the LiteralControl while browsing through the MSDN docs and that my friends are a real treat. It lives in the System.Web.UI namespace and it adds great value. You can use it to achieve the same task as the one above, but in a more natural way.

Page.Controls.Add(new LiteralControl(“Hello world”));

It’s much cleaner, much nicer and much simpler. Now I can’t live without it.

Comments

Tag:

Add to Del.icio.us | Digg | Yahoo! My Web | Furl

Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

Top Rated White Papers and Resources

What do you think? Respond.

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>