Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » Server Controls » Apply Styles to Web Controls

Apply Styles to Web Controls

In this article, I will show you how you can use the .NET Framework class library’s Web classes to create and apply styles on Web Controls programmatically.

Author Rank :
Page Views : 6203
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Gauge for SharePoint
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

There are different ways to apply styles on Web controls. One of the most commonly used method is using style sheets. You can also apply styles of Web controls programmatically and the .NET Framework class library provides classes to do so. In this article, I will show you how you can use the .NET Framework class library's Web classes to create and apply styles on Web Controls programmatically. 

The System.Web.UI.WebControls.Style Class

The Style class defined in the System.Web.UI.WebControls namespace represents the style of a Web server control. This class provides the properties that can be used to set the appearance of a Web server control and can be applied to multiple Web server controls to provide a common appearance. Using these properties, you can set the background, foreground colors, border width and border styles and size of the Web server control. Table 1 describes the Style class properties. 

Table 1. The Style class properties

BackColor Gets or sets the background color of the Web server control
BorderColor Gets or sets the border color of the Web server control
BorderStyle Gets or sets the border style of the Web server control
BorderWidth Gets or sets the border width of the Web server control
CssClass Gets or sets the CSS class rendered by the Web server control on the client.
Font Gets and sets the font properties associated with the Web server control
ForeColor Gets or sets the foreground color (typically the color of the text) of the Web server control.
Height Gets or sets the height of the Web server control.
Width Gets or sets the width of the Web server control.

The System.Web.UI.WebControls.WebControl.ApplyStyle Method

The ApplyStyle method of the WebControl class is responsible for applying a Style object to a Web control. This method takes a parameter of Style object. For example, the following code snippet shows you how to call the ApplyStyle method. 

WebControl ctrl;
Style s;
// Set the Style properties here
...........
// Apply Style
ctrl.ApplyStyle(s); 

The Example 

Now I will show you how to apply styles on Web controls. To test this,  I create a Web application using Visual Studio .NET and add three controls to the Form - a Button, a TextBox, and a ListBox. I also change the properties of the Button control and add few items to the ListBox using the Collection property of ListBox from the Properties window. The final Web Form page looks like Figure 1.

 

Now I create two methods - CreateStyle and SetControStyle. The CreateStyle method takes seven parameters for style such as background color, foreground color, border width, and font styles. This method returns the Style object as you can see from Listing 1.

The SetControlStyle method takes two parameters - WebControl, and Style and calls the ApplyStyle method of WebControl as you can see from Listing 1. 

Listing 1. 

// This method creates a new style. To update the style
// of your controls, update this method.
private Style CreateStyle(Color backClr, Color foreClr, int borderWidth, string fntName, int fntSize, bool fntBold, bool fntItalic
/* You can add more parameters to the method */)
{
Style s =
new Style();
s.BackColor = backClr;
s.ForeColor = foreClr;
s.BorderWidth = borderWidth;
s.Font.Name = fntName;
s.Font.Size = fntSize;
s.Font.Bold = fntBold;
s.Font.Italic = fntItalic;
return s;
}
// This method applies a Style object to a WebControl
private void SetControlStyle(System.Web.UI.WebControls.WebControl ctrl,Style s)
{
ctrl.ApplyStyle(s);
}

Now I create the Set Style button click event handler by simply double clicking on the button. On this button click event handler, I call CreateStyle and SetControlStyle methods for different controls as you can see from Listing 2. 

Listing 2.

private void Button1_Click(object sender, System.EventArgs e)
{
Style st = CreateStyle(Color.Green, Color.Yellow, 3,
"Verdana", 10,
true, true);
SetControlStyle(TextBox1, st);
st = CreateStyle(Color.Red, Color.Black, 2,"Verdana", 12,
true, true);
SetControlStyle(SetStyleBtn, st);
st = CreateStyle(Color.Blue, Color.Yellow, 2,"Verdana", 12,
true, true);
SetControlStyle(ListBox1, st);
}

Now when I run the sample and click the Set Style button, the final Form looks like Figure 2. 

 

Conclusion

In this article, you saw how easy is it to apply styles to Web controls programmatically.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.