Click here to Skip to main content
15,879,348 members
Articles / Web Development / ASP.NET

CSS Custom Provider To extend Web Control's Property Window

Rate me:
Please Sign up or sign in to vote.
4.25/5 (12 votes)
31 Dec 20035 min read 61.8K   1.4K   28  
This article describes how to extend the design time features of Visual studio to expedite development process.
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls; 
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Drawing.Design;

namespace AV.UI.Controls
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	[ProvideProperty("MyCSSName",typeof(WebControl))] 
	public class CSSNameProvider : Component, IExtenderProvider
	{
		public CSSNameProvider()
		{
			//Trace.Listeners.Add(new EventLogTraceListener("CSSNameProvider"));
			CSSFileName = "";
			this.MyCSSName = "";
		}

		public bool CanExtend(object extendee)
		{
			//Trace.WriteLine(extendee.ToString());
			return (extendee is WebControl);
		}

		private string m_CSSClass;

		[Browsable(false)]
		[Description("Gets or Sets the CSS Class")]
		[Editor(typeof(CSSNameEditor),typeof(UITypeEditor))]
		public string MyCSSName
		{
			get 
			{ 
				//Trace.WriteLine("In get method ");
				return m_CSSClass;
			}
			set 
			{
				//Trace.WriteLine("In set method ");
				m_CSSClass = value;
			}
		}

		private static string m_CSSFile;

		[Browsable(false)]
		[Description("Gets or Sets the CSS File")]
		[Editor(typeof(CSSFileEditor),typeof(UITypeEditor))]
		public static string CSSFileName
		{
			get 
			{ 
				//Trace.WriteLine("In get file method ");
				return m_CSSFile;
			}
			set 
			{
				//Trace.WriteLine("In set file method ");
				m_CSSFile = value;
			}
		}

		[Browsable(true)]
		[Description("Gets or Sets the CSS File")]
		[Editor(typeof(CSSFileEditor),typeof(UITypeEditor))]
		public string CSSFile
		{
			get 
			{ 
				//Trace.WriteLine("In get file method ");
				return CSSFileName;
			}
			set 
			{
				//Trace.WriteLine("In set file method ");
				CSSFileName = value;
			}
		}

		[Browsable(true)]
		[Description("Gets or Sets the CSS Class for the control.")]
		[Editor(typeof(CSSNameEditor),typeof(UITypeEditor))]
		public string GetMyCSSName(System.Web.UI.Control control)
		{
			//Trace.WriteLine("In GetMyCSSName method ");
			return MyCSSName;
		}

		public void SetMyCSSName(System.Web.UI.Control control, string myCSS)
		{
			//Trace.WriteLine("In SetMyCSSName method ");
			WebControl myControl = (WebControl)control;
			this.MyCSSName = myCSS;
			myControl.CssClass = myCSS;
		}

		public string PickCSSFileName()
		{
			string fileName = String.Empty;
			OpenFileDialog dlg = new OpenFileDialog();
			//Trace.WriteLine("Current Directory " + Environment.CurrentDirectory.ToString());
			dlg.InitialDirectory = @"C:\Inetpub\wwwroot\";
			dlg.Filter = "css files(*.css)|*.css";
			dlg.FilterIndex = 1;
			if(dlg.ShowDialog() == DialogResult.OK)
			{
				fileName = dlg.FileName;
			}
			return fileName;
		}

		public static string PickCSSName()
		{
			string cssName = String.Empty;
			
			if(CSSFileName == "")
			{
				MessageBox.Show("You must select a CSS file using Provider Property window.");
				return cssName;
			}
			
			CSSClassViewer viewer = new CSSClassViewer(CSSFileName);
			viewer.ShowDialog(); 
			cssName = viewer.CSSName;
			return cssName;
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
I am working as Senior Software Designer and Developer for last 3.5 years. I like reading tech articles and writing them, mainly .Net stuff.

I completed MCAD, MCSD.Net, MCSE-Win2k, MCDBA.

Comments and Discussions