Pages

Jun 18, 2008

How to Implement themes to GridView in asp.net2.0

Introduction:
The theme of this article is Themes. I will show you that what Themes are used for and how you can make you own Themes quickly and easily. This is a multi series article so stay tuned for the rest of the series. What's up with Themes? Hey! we got CSS (Cascading Style Sheets) so why do we need Themes? The thing about CSS is that it only exposes some fixed style properties which we can use. If we want to change some property like AlternatingItemStyle of the GridView control we will not be able to do this by using simple CSS. Themes allow you to change the control properties. This mean you can change most of the properties exposed by any server control in ASP.NET 2.0.

Gettting Started With Themes: Let's get started with Themes. The first thing that you need to do is to add a skin file. Once you try to add a new skin file ASP.NET will make a folder called App_Themes in which all Themes will be placed. After the App_Themes folder has been created you can simply add .skin files inside the App_Themes folder. You can name the Theme files according to their action. Like if you are adding a Theme which makes the appearance of your page orange you can name is [YourSideName]_Orange_Theme. You can name it anything you want from orange Theme to "Yellow Banana" Theme.

What is in that Skin?
Skin files contains the definition of the server controls on which the Themes will be applied. Here is my Skin file called "Red" whose purpose is to make the GridView red.

As, you might have already noticed that the GridView definition does not contain the ID attribute. That is because this Theme is applied to all the GridViews on the page. There are couple of ways that you can apply different Themes for the same server control.

Applying Themes: There are various ways to apply the Theme to the page. The simplest one of them is to use the Page directive to apply the Theme to the current page.
The above will apply the Theme to the current page. If you wish to apply the same Theme to all the pages of your website then it is a better idea to define the Themes in the configuration file.
Eg:




You can also apply Themes dynamically using the Page.Theme property. The thing to remember about applying the Themes programmatically is that you can only apply it inside the Page_PreInit event.
Check out the code below which applies the Theme at runtime. protected void Page_PreInit(object sender, EventArgs e)
{
Page.Theme = "Green";
}

One thing that you need to remember is that when you set the Theme at different stages the Theme that is set dynamically takes precedence over the Page directive and Web.config. This means that if you have define your Theme to be "Blue" in Web.config and "Green" is Page directive and "Red" dynamically then the Theme set for the page will be set "Red". There is much more to cover in Themes which will be covered in later articles.
Reference: GridViewGuy



5 comments:

Seshadri said...

How can headerstyle for a TemplateField or BoundField be defined in a skin

Mukund The Lion said...

Yeah Obviously, Template or BoundField HeaderStyle however relay on the same headerStyle Property that we describe inside the GridView Tag(as shown in Eg.).
Since the HeaderStyle property is common for GridView whatever we give inside Headerstyle tag will be applied to all column Headers even it is Template or BoundField.

Manikandan said...

Your article is not so impressive and informative. This is just a suggestion.

vanitha said...

This is good to see ur thoughts.do you have any idea about how to give header border i,e inside the cell just four corner of each cell in the gridview without backcolor.

Anonymous said...

The Kettic GridView offers built-in themes to the users to apply custom style and appearance to the grid for their applications.