opular applications including ICQ, WinAmp, and Windows Media Player use skins to enable a user to choose another appearance for that application. The skins for those applications can be very simple, or have outrages themes, such as a particular movie. There’s a very cute Winnie the Pooh skin for ICQ, for instance. From this I’m tempted to say that skins are just plain fun (or foolishness), but there is actually a practical side to skins. If you build a site with controls that allow skinning, you can easily modify the user interface for that site. This goes beyond simple changes to colors and fonts, which I discussed in “Creating ASP.NET Controls with Style“
in the September/October 2003 issue of CoDe Magazine. Skinned controls let you change the position and order of the controls, and add other elements to the control such as a background image or a company logo. Creating controls this way also gives you the opportunity to reuse the functionality of controls in different applications. This is great if you’re a solutions provider that often builds sites for customers that are more or less the same in functionality. Just put a designer to work and the whole site is ready to go in a completely different style. Because you’re not bound to just coloring and fonts, you can get away from the uniform look and feel for similar sites that you see so often. You can, of course, also use different sets of skins in the same application, which lets users pick a look that satisfies them. Choosing different looks for different departments within a company is also a great use for skins, and I’m sure that I could come up with many more uses. So let’s get down to business and look at how skinned controls work and how you can create them yourself.
Fast Facts |
A skinned control enables you to create different views for the same control. This allows you change the look of controls at runtime, allowing your users to pick a skin that suits their individual taste, or allows you to implement a uniform look by department, group, etc. |
Skinned Control Basics
|
A skinned control consists of two parts: a control and a skin. The control is a regular control, but with some added functionality to allow skinning. The skin is a user control (.ascx file) with only formatting. When properly wired up, you can also add code but you should only do that if a certain skin requires this. The whole idea behind skinned controls is that you can separate the code and presentation into separate files, similar to the way you would do this with code behind. Adding code to the skin would go against this idea.
When you use a skinned control, you add the control to a page. When that page loads, the control loads the skin, adds the skin to its controls collection, and then wires up the controls in the skin to the child controls of the control. This is different from a normal server control, where child controls are added to the controls collection “by hand.”
Creating a Base Control
The best way to create skinned controls is by letting them inherit from a base class that implements the basic features a skinned control needs. Listing 1 shows the code implementing this base class, which is an abstract class (that is, it needs to be inherited) named SkinnedControlBase. Note that the base class inherits from Control, not from WebControl. You would use the WebControl class only if you create a custom control that has visible output, and which should support styling. Technically that is also true for a skinned control, but it uses the skin to actually display itself. There is no display logic in the control itself, and any styling properties associated with the WebControl class would serve no purpose. Because the control will contain child controls, it also needs to implement the INamingContainer interface, so each child control will get a unique name within the page.
One property that all skinned controls need is, of course, where to find the skin. Since this is something that should be configurable, this should be a property of the control. In Listing 1 the SkinPath property serves this purpose. Note that the value of the SkinPath property is not saved in the ViewState, so a change to another skin at runtime will not be persistent. Don’t worry about this, however, because in most cases you either have a specific set of skins, or you select the skins based on some profile.
|
The CreateChildControls method plays an important role in skinned controls. With “normal” server controls, this is the place where you would add child controls to the controls collection. With a skinned control, this is where you add the skin to the controls collection and then wire up the child controls of the control to those in the skin. Before you add the skin, you should clear the control tree and initialize it by calling the base method, to make sure that any controls added during earlier phases in the page’s execution are removed. Then you lookup the skin and add it to the controls collection. You don’t need to register the user control with the page you’re using it in because the skinned control does the work to retrieve the user control. This is logical; otherwise you would need to register each skin with the page, making skins much less useful.
Note that to keep the sample code short, I didn’t add any error handling code. You should, however, make sure that the skin you want exists and that it initializes correctly. During initialization, my sample code wires the child controls to controls in the user control. You’ll have an error if a child control of the control doesn’t exist in the skin, or if you use the wrong control type. You should properly handle each case so the skin designer knows what he or she is doing wrong. The initialize process should always occur, but it is specific to the control. I took the extra step to define an abstract method named Initialize, which the actual control should override for wiring up the controls. Alternatively you could tag the child controls of a skinned control with custom attributes, and use reflection to handle this task from within the base class. Then control developers don’t need to worry about wiring up child controls in the Initialize method. However, such a discussion is beyond the scope of this article.
Creating the Skinned Control
With the base control in place, now we can implement an actual skinned control. I’ve found that once I’ve implemented the base class, creating a skinned control is easier than creating a non-skinned control because you don’t have to add controls, formatting, and such with the Render, RenderContents, RenderChildren, or CreateChildControls methods. That is all part of the skin, so you can actually focus 100% on adding the functionality the control should have.
Listing 2 shows the code of a skinned control inheriting the base control defined in Listing 1. In this case, I’ve created a LoginBox control that consists of a username TextBox, a password TextBox, and a login Button. Each of these controls is declared globally within the class. I’ve also defined a default skin path in case the designer doesn’t add one. In the constructor, my code checks the skin path given by the designer, and if it’s not there, the controls will use the default skin path. If you intend to use a skin defined in a profile, use the default skin path to implement that. You can also implement the default skin path in a constructor of the base class, but I’ve found this to be tricky at best.
The LoginBox control contains two string properties: Username and Password. The values of these properties correspond to the Text properties of the username and password TextBox controls. Before accessing these controls, you need to make sure that they’ve been added to the control tree by calling EnsureChildControls. If you don’t do that, you may get an erroneous value when accessing the values of the Username or Password TextBox, providing you don’t get an error first. The LoginBox control also contains a Login event to notify the user of the control that someone pressed the login button. Read the sidebar, “Event Handling,” to learn how I handle events in a skinned control (or a composite control). In Listing 4, which I’ll discuss later, you can see that I attached event handling code to the event exposed by the LoginBox.
Last but not least, the LoginBox control overrides the Initialize method of the base control. You can see that I attach the privately declared child controls of the LoginBox control to the corresponding control in the skin using the FindControl method. As I mentioned earlier, this is where things can go wrong. The designer needs to use exactly the same type and name for controls defined in the skinned control. This means that you need to document which child controls are part of the skinned control, and what their names are.
Creating a Skin
![]() |
devx-admin
Share the Post:
![]() ![]() Top Software Development Companies
Noah Nguyen
September 30, 2023
Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in ![]() ![]() Top Web Development Companies in India
Jordan Williams
September 30, 2023
In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, ![]() ![]() Top Web Development Companies in USA
Johannah Lopez
September 30, 2023
Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner ![]() ![]() Inside Michigan’s Clean Energy Revolution
Grace Phillips
September 29, 2023
Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the ![]() ![]() European Chips Act: What is it?
Lila Anderson
September 29, 2023
In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor ![]() ![]() You Should Use Low-Code Platforms for Apps
Jordan Williams
September 29, 2023
As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with ![]() ![]() Top Software Development Companies
Noah Nguyen
September 30, 2023
Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in ![]() ![]() Top Web Development Companies in India
Jordan Williams
September 30, 2023
In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to ![]() ![]() Top Web Development Companies in USA
Johannah Lopez
September 30, 2023
Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your ![]() ![]() Inside Michigan’s Clean Energy Revolution
Grace Phillips
September 29, 2023
Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting ![]() ![]() European Chips Act: What is it?
Lila Anderson
September 29, 2023
In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its ![]() ![]() You Should Use Low-Code Platforms for Apps
Jordan Williams
September 29, 2023
As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not ![]() ![]() Five Powerful Strategies to Bolster Your Cybersecurity
Noah Nguyen
September 29, 2023
In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies ![]() ![]() Tech Layoffs Are Getting Worse Globally
Jordan Williams
September 29, 2023
Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data ![]() ![]() Huawei Dazzles with Electric Vehicles and Wireless Earbuds
Johannah Lopez
September 29, 2023
During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting ![]() ![]() Digital Banking Needs Cybersecurity
Johannah Lopez
September 29, 2023
The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising ![]() ![]() Terry Clune’s Fintech Empire
Grace Phillips
September 29, 2023
Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned ![]() ![]() The Role Of AI Within A Web Design Agency?
DevX Editor
September 29, 2023
In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing ![]() ![]() The Future of AI and the Investment Opportunities it Presents
DevX Editor
September 29, 2023
There is no doubt that modern technology has changed the way we live and work forever. Nowadays, there is a wide array of different types of technologies such as AI ![]() ![]() Is Generative AI the Next Internet?
Lila Anderson
September 29, 2023
The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These ![]() ![]() The New Surface Laptop Studio 2 Is Nuts
Noah Nguyen
September 29, 2023
The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over ![]() ![]() GPU-Accelerated 5G in Japan
Johannah Lopez
September 28, 2023
NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will ![]() ![]() AI Journalism: Balancing Integrity and Innovation
Grace Phillips
September 28, 2023
An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These ![]() ![]() Big Deal Days Extravaganza
Lila Anderson
September 28, 2023
The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created ![]() ![]() Cisco Splunk Deal Sparks Tech Acquisition Frenzy
Jordan Williams
September 28, 2023
Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the ![]() ![]() Iran’s Jet-Propelled Drone Reshapes Power Balance
Jordan Williams
September 28, 2023
Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional ![]() ![]() Did the Overshoot Commission Shoot Down Geoengineering?
Johannah Lopez
September 28, 2023
The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to ![]() ![]() Revolutionizing Remote Learning for Success
Noah Nguyen
September 28, 2023
School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which ![]() ![]() SABERS Batteries Transforming Industries
Grace Phillips
September 28, 2023
Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the ![]() ![]() How Much Does It Cost to Build a Website?
DevX Editor
September 28, 2023
Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted ![]() ![]() Battery Startups Attract Billion-Dollar Investments
Lila Anderson
September 28, 2023
In recent times, battery startups have experienced a significant boost in investments, with three businesses obtaining over $1 billion in funding within the last month. French company Verkor amassed $2.1 |