Library tutorials & articles
Making skinned custom controls
- Overview
- The base class - SkinControl
- A simple sample control
- Using the sample control
The base class - SkinControl
Now let's get started on the actual code… First up is the base class all your skinned controls will derive from; the abstract class SkinControl. using System;
using System.Web.UI;
namespace MySite.Controls
{
public abstract class SkinControl : Control, INamingContainer
{
// ***********
// VARIABLES
// ***********
private Control skinControl = null;
private string skinVirtualPath = null;
// ************
// PROPERTIES
// ************
public string SkinVirtualPath
{
get { return skinVirtualPath; }
set { skinVirtualPath = value; }
}
// *********
// METHODS
// *********
protected override void CreateChildControls()
{
Controls.Clear();
base.CreateChildControls();
try
{
// Try to load the skin
PreInitialize();
// Add the skin to the ControlCollection
Controls.Add( skinControl );
// Initialize the skin
Initialize( skinControl );
}
catch {} // Prevent application from breaking
}
private void PreInitialize()
{
skinControl = Page.LoadControl( skinVirtualPath );
}
protected abstract void Initialize(Control skinControl);
}
}
As you can see our base class derives from Control class and implements the interface INamingContainer. The reason we implement INamingContainer is that it makes sure our controls are named in a hierarchical, correct manner.
The first thing after declaring the namespace and class is to add 2 variables which we will use to store the actual skin control with and the virtual path to the skin file. After this is taken care of we expose a public property called SkinVirtualPath. This property will allow page developers to get and set the virtual path to the skin file - each control will need to have this set in order for the control to work.
Next on the agenda is the part where we override CreateChildControls method and do a little action – first off we clear the control collection by performing a call to Controls.Clear() method, and then we do a call to the base class' CreateChildControls method.
Once this is over with we need to add our actual skinning methods, namely PreInitialize and Initialize and we need to add the skin control to the control collection. PreInitialize in its current state does nothing more than instantiating skinControl variable by using the method Page.LoadControl. This method takes 1 parameter - a string that point to the virtual path where the user control resides.
The Initialize method is abstract because it needs to be specifically overridden by each control that derives from SkinControl. This is because each skin uses its own set of html- and web controls and thus trying to find controls that might not exist make no sense…
Related articles
Related discussion
-
Profile Class does not work after Translation
by converter2009 (1 replies)
-
what is the SQL Server Provider
by hayperaktib (1 replies)
-
Very Urgent regarding deleting the images from a folder
by Nanosteps (6 replies)
-
Java Script, File uploading on ftp server using java script code
by h_c_a_andersen (2 replies)
-
sharepoint calendar web part with events from sql table
by converter2009 (2 replies)
Related podcasts
-
StackOverflow uses ASP.NET MVC - Jeff Atwood and his technical team
Scott chats with Jeff Atwood of CodingHorror.com and most recently, StackOverflow.com. Jeff and Joel Spolsky and their technical team have created a new class of application using ASP.NET MVC. What works, what doesn't, and how did it all go down?
Events coming up
-
Mar
15
DevWeek 2010
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.
You have successfully plundered the concepts contained in the asp.net forums without offering anything new, or really showing the reader why they would want to use them
Not to mention your wonderful coding practice of catch {}
What happens if the InitialiseSkin method makes calls to controls that don't exist on the skin? The skin is just not going to load, it won't show the user any helpful message. You're forgetting the skills of the average developer probably don't encompass debugging stack traces which don't get rethrown.
Nice one.
This thread is for discussions of Making skinned custom controls.