ARTICLE

Adding CSS to Themes in ASP.Net

Posted by Gurjeet Singh Articles | ASP.NET using VB.NET June 14, 2011
Cascading Style Sheets (CSS) load pages faster and leaner, this is main advantage of CSS file. CSS is alternative of skins, to change the appearance of ASP.Net control and HTML element using CSS file. If you add CSS file in Theme folder than it’s automatically applied on all pages of your application. CSS used to style HTML element. ASP.Net render HTML this is the reason that CSS file easily apply on ASP.Net control.
Download Files:
 
Reader Level:

Cascading Style Sheets (CSS) load pages faster and leaner, this is main advantage of CSS file. CSS is alternative of skins, to change the appearance of ASP.Net control and HTML element using CSS file. If you add CSS file in Theme folder than it's automatically applied on all pages of your application. CSS used to style HTML element. ASP.Net render HTML this is the reason that CSS file easily apply on ASP.Net control.
Suppose you change a few property of one label control, whenever you request page only additional properties must be rendered to the browser but in case of SKIN file first all label property change in application then after that label rendered to the browser, so we can say CSS faster as compare to SKIN file.
Using CssClass attribute of ASP.Net control you can apply any style by target any control. In this example, I have applied different appearance on all controls using style sheet. First must be look blue and second label must be look green. One interesting thing, Look at the output in figure3 you will notice that password is displaying in white color because I set its color property white in CSS file.
Step1: Add Theme by adding a new folder to a special folder in your root application. Right clicking on project in Solution Explorer drag your mouse on Add Asp.Net Folder you will see Theme option in list and click on that. See the below figure to know how to add theme in application. Different themes should be added on different folder in App_Themes folder. You can add multiple subfolders in Theme folder. A theme file contains different type of files but Skin and CSS file is most important file in Theme folder.


1AddTheme.gif

Figure 1

Step 2: After adding Theme Folder add CSS file on that folder, right click on theme folder than click on Add New Item and select StyleSheet file from dialog box.

2.AddCss.gif

Figure 2

Login.aspx
<%@ Page Language="C#" StylesheetTheme="Theme1" AutoEventWireup="true" CodeFile="LoginPage.aspx.cs" Inherits="LoginPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
      <title></title> 
</head>
<body>
    <form id="form1" runat="server">
    <div class="area">  
    <asp:Label ID="Label1" CssClass="labl" runat="server" Text="User Name"></asp:Label>
    <asp:TextBox ID="TextBox1" CssClass="txtbox1" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label2" CssClass="labl2" runat="server" Text="Password"></asp:Label>
    <asp:TextBox ID="TextBox2" CssClass="txtbox2" TextMode="Password" runat="server"></asp:TextBox>
        <br />
    <asp:Button ID="Button1" CssClass="btn" runat="server" Text="Sign In" />
    </div>    
    </form>
</body>
</html>

App_Themes/Theme1/StyleSheet.css

 html
{
   background-color:Silver;
}

.area
{
  margin-left:100px;
    width:300px;
    height:180px;
    background-color:White;
}

.labl
{
    margin-left:20px;
    color: Blue;
    font-size: 15px;
}

.labl2
{
    margin-left:20px;
    color: Green;
    font-size: 15px;
}

.txtbox1
{
    margin-left: 50px;
    margin-top: 10px;
    background-color: Yellow;
    border-style: dashed;
}

.txtbox2
{
    margin-left: 60px;
    margin-top: 20px;
    background-color: Aqua;
    border-style:dashed;
    border-color:Black;
    color:White;
}

.btn
{
   background-color:Fuchsia;
   margin-top:40px;
   margin-left:100px;
}

Output:

3.output.gif

Figure 3

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor