Click here to Skip to main content
15,896,111 members
Articles / Web Development / CSS

Dynamic CSS Styling in ASP.NET: A Flexible Approach

Rate me:
Please Sign up or sign in to vote.
4.08/5 (20 votes)
6 May 2006CPOL5 min read 165.8K   1.8K   33  
A flexible approach towards dynamic styling in ASP.NET. This technique emphasizes structure and extensibility.
using System;
using System.Web.UI.HtmlControls;

namespace DynamicStyling
{
	/// <summary>
	/// Summary description for BasePage.
	/// </summary>
	public class BasePage : System.Web.UI.Page
	{
		#region Members and Properties
		private string _StyleClass;
		public string StyleClass
		{
			get
			{
				return _StyleClass;
			}
			set
			{
				_StyleClass = value;
			}
		}
		//More Members Here
		#endregion

		/// <summary>
		/// This adds a class attribute to the Form element called Form1
		/// in any inheriting ASP.Net page.
		/// </summary>
		private void styleForm()
		{
			HtmlForm Form1 = (HtmlForm)this.FindControl("Form1");
			Form1.Attributes.Add("class", _StyleClass);
		}

		public BasePage()
		{		
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender (e);
			//give the Form element the appropriate style class
			styleForm();
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Lebanon Lebanon
Over 5 years of experience as a Web Developer using ASP.NET.
Appreciates Good Design and continually seeks to improve his methods.

MCP in Developing Web Applications using ASP.NET, and in XML Web Services and Server Components.

Comments and Discussions