You are on page 1of 8

G.N.D.

U AMRITSAR
CET DEPARTMENT
PROGRAMMING IN ASP.NET
ASSIGNMENT-1

SUBMITTED TO:
Er. Avneet Kaur

SUBMITTED BY:
Nandini
17032001507
B.Tech CSE(Sem-5)
ASP.NET Web Forms - Master Pages

Master pages provide templates for other pages on your web site.

Master Pages
Master pages allow you to create a consistent look and behavior for all the pages (or group of
pages) in your web application.

A master page provides a template for other pages, with shared layout and functionality. The
master page defines placeholders for the content, which can be overridden by content pages.
The output result is a combination of the master page and the content page.

The content pages contain the content you want to display.

When users request the content page, ASP.NET merges the pages to produce output that
combines the layout of the master page with the content of the content page.

Master Page Example


<%@ Master %>

<html>
<body>
<h1>Standard Header From Masterpage</h1>
<asp:ContentPlaceHolder id="CPH1" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>

The master page above is a normal HTML page designed as a template for other pages.

The @ Master directive defines it as a master page.

The master page contains a placeholder tag <asp:ContentPlaceHolder> for individual


content.
The id="CPH1" attribute identifies the placeholder, allowing many placeholders in the same
master page.

This master page was saved with the name "master1.master".

Note: The master page can also contain code, allowing dynamic content.

Content Page Example


<%@ Page MasterPageFile="master1.master" %>

<asp:Content ContentPlaceHolderId="CPH1" runat="server">


<h2>Individual Content</h2>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</asp:Content>

The content page above is one of the individual content pages of the web.

The @ Page directive defines it as a standard content page.

The content page contains a content tag <asp:Content> with a reference to the master page
(ContentPlaceHolderId="CPH1").

This content page was saved with the name "mypage1.aspx".

When the user requests this page, ASP.NET merges the content page with the master page.

Note: The content text must be inside the <asp:Content> tag. No content is allowed outside the tag.

Content Page With Controls


<%@ Page MasterPageFile="master1.master" %>

<asp:Content ContentPlaceHolderId="CPH1" runat="server">


<h2>W3Schools</h2>
<form runat="server">
<asp:TextBox id="textbox1" runat="server" />
<asp:Button id="button1" runat="server" text="Button" />
</form>
</asp:Content>

The content page above demonstrates how .NET controls can be inserted into the content
page just like an into an ordinary page.

Themes in ASP.NET
A theme decides the look and feel of the website. It is a collection of files that define
the looks of a page. It can include skin files, CSS files & images.

We define themes in a special App_Themes folder. Inside this folder is one or more
subfolders named Theme1, Theme2 etc. that define the actual themes. The theme
property is applied late in the page's life cycle, effectively overriding any
customization you may have for individual controls on your page.

How to apply themes

There are 3 different options to apply themes to our website:


1. Setting the theme at the page level: the Theme attribute is added to the page
directive of the page.
1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Defa
ult.aspx.cs" Inherits="Default" Theme="Theme1"%>
2. Setting the theme at the site level: to set the theme for the entire website you
can set the theme in the web.config of the website. Open the web.config file and
locate the <pages> element and add the theme attribute to it:

1. <pages theme="Theme1">
2. ....
3. ....
4. </pages>
3. Setting the theme programmatically at runtime: here the theme is set at runtime
through coding. It should be applied earlier in the page's life cycle ie.
Page_PreInit event should be handled for setting the theme. The better option
is to apply this to the Base page class of the site as every page in the site
inherits from this class.

Page.Theme = Theme1;
Uses of Themes
1. Since themes can contain CSS files, images and skins, you can change colors,
fonts, positioning and images simply by applying the desired themes.

2. You can have as many themes as you want and you can switch between them
by setting a single attribute in the web.config file or an individual aspx page.
Also you can switch between themes programmatically.

3. Setting the themes programmatically, you are offering your users a quick and
easy way to change the page to their likings.

4. Themes allow you to improve the usability of your site by giving users with
vision problems the option to select a high contrast theme with a large font size.

SKINS IN ASP.NET

What ASP.Net Skins are

ASP.Net skins can only be used to apply the styles to the ASP.Net controls.so in this
article let us see the procedure for using ASP.Net Skins.

How to Add ASP.Net themes


Add an ASP.Net Themes Folder
To use the themes in the web site, we need to add an ASP.Net Themes folder by
right-clicking on Solution Explorer as in the following:
After adding the theme folder, add the SkinFile.skin file by right-clicking on the
ASP.Net theme folder. The Solution Explorer will then look as follows:

Now add the ASP.Net controls inside the SkinFile.Skin and assign the Style to the
controls using their properties as in the following:

In the code above I have used just a few controls and properties for assigning the
styles but you can assign it for every ASP.Net control that you want, such as in the
following:
Note:

1. A control Id cannot be assigned to ASP.Net controls inside the SkinFile.skin.


2. SkinId must be assigned to the ASP.Net controls inside the SkinFile.skin.
3. The SkinId should be uniquely defined because duplicate SkinId's per control
type are not allowed in the same theme.
4. Only one default control skin per control type is allowed in the same theme.

I hope you understand the basic concepts from the preceding explanation.

How to Use ASP.Net Skins

To use existing ASP.Net Skins in an ASP.Net page you need to assign the existing
theme at page level as in the following.

In the preceding source code, we are assigning the existing ASP.Net Skin File at
page level, the existing ASP.Net Skin automatically appears in the box after using
the Themes property in the page header.

Assigning the Skin to the ASP.Net Controls

To assign the skin to the ASP.Net controls, you need to assign it to the control's
SkinId Property as in the following:

In the preceding ASP.Net source code, we used the skinId property of the control to
assign the existing skin.

Now run the application, it will appear as in the following:


Advantages of ASP.Net Skins

1. Skins can be used to apply the common style to the individual ASP.Net
controls.
2. You can apply From the preceding example, we learned how to use skins.
a style to the specific control.
3. It easy to remind which skin is for which control because inside the skin file
ASP.Net controls are directly used and in .aspx pages it automatically appears
to that control id style.
4. Gives the same consistency to the multiple controls in the web application.
Note
• When you use an ASP.Net Skin you need to add a Skinfile.skin file under the
ASP.Net theme folder.

You might also like