Library tutorials & articles
Making skinned custom controls
- Overview
- The base class - SkinControl
- A simple sample control
- Using the sample control
A simple sample control
Ok, now let's test out our base class by creating a simple sample control. The control will do nothing more than display some text, using a label control and a repeater control. using System;
using System.Web.UI.WebControls;
namespace MySite.Controls
{
public class SampleControl : SkinControl
{
// ***********
// VARIABLES
// ***********
private string skinVirtualPath = "SampleSkin.ascx";
// *************
// CONSTRUCTOR
// *************
public SampleControl()
{
if (SkinVirtualPath == null)
SkinVirtualPath = skinVirtualPath;
}
// *********
// METHODS
// *********
protected override void Initialize(System.Web.UI.Control skinControl)
{
Label label1 = (Label) skinControl.FindControl("Label1");
Repeater repeater1 = (Repeater) skinControl.FindControl("Repeater1");
label1.Text = "A few primes";
repeater1.DataSource = new string[] { "1", "3", "5", "7", "11" };
repeater1.DataBind();
}
}
}
The first thing we do besides of course declaring the class is to hard-code the default virtual path to the skin file for this specific control. In this case SampleSkin.ascx. Once this is done we add the constructor for this class and inside the constructor we need to check whether a virtual path to a skin file is set, if not we use the default path.
The most important thing comes next, and that is overriding the Initialize method to customize it for the control. In the method we first of all locate all the controls in the skin we want to control – in this case a label control and a repeater control. The retrieval process is done by taking advantage of the FindControl method. For this sample the controls in the skin are named Label1 and Repeater1…
After we have found our controls we can customize the controls like we see fit.
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.