Aidan Garnish
MOSS 2007 and other Microsoft technologies

An efficient way to set web control values

December 10, 2007 11:11 by aidan

Rather than setting the widths of label and textbox controls individually like this:

oLabel1.Width=150;

oLabel2.Width=150;

oLabel3.Width=150;

It is much more efficient to add the controls to the controls collection and use:

foreach(Control oControl in this.Controls)
{
     if (oControl.GetType().ToString() == "System.Web.UI.WebControls.Label")
     {
       Label oLabel = (Label)oControl;
       oLabel.Width = 120;
     }
}

This then allows you to change the widths of all your labels within the page or web part in one place. This approach can also be taken with other controls like text boxes and drop downs or could be used to set other properties such as font or color.

Or you could just use CSS.  Smile


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories: ASP.Net
Actions: E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Related posts

Comments

June 5. 2008 17:17

Why don't you use the statment "is" like:

if (oControl is Label)
.....

?

layos

Add comment


 

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

January 6. 2009 15:25