ASP.Net – Global page and Master page

Standard

Global.aspx page:

First file rendered when hit the application. This ‘Global’ class inherited from ‘System.Web.HttpApplication’ It has five events such as

  1. Application_Start

This event fired when application startup. Useful to initialize application specific (irrespective to the session) variables.

Example : Application[“StartedAt”] = DateTime.Now.ToShortDateString();

  1. Application_End

This event fired when application end. For example to write log with application ended time for reference.

  1. Application_Error

This event fired when application got error.

  1. Session_Start

This event fired when application session start. Useful to initialize the session and session specific variables. For example: session id, username, roles, user specific variables such as themes , skins …etc., variables used multiple pages.

Session[“User Name”] = string.Empty;

Session[“Roles”] = string.Empty;

  1. Session_End

This event fired when application session end. Useful to clear the session and session specific variables.

Master Page:

Master page is just like HTML frameset (More than one pages displayed in one browser screen) display two types information such as static information displayed throughout the site, dynamic contents (information) specific to the page. It helps to display the common information such as company logo, company name, system name, copyright information, common controls such as ‘Home’ and ‘Back’ buttons, menu control and site navigation control, common functionality and maintain look and feel of the site.

[Tested in Visual web developer 2005 express edition]

Step One:

Create the web site as you wish with default page

Step Two:

Either right click on the project in the solution explorer or use Website menu to select

‘Add New Item’ to select and add ‘Master page’ in your site.

Step Three:

Delete the default page. Then recreate the page using above step (Step Two) but select ‘Web form’. Before click ‘Add’ button in the screen check the ‘Select Master Page’ check box just above this button. Content page will be created with master page.

Step Four:

Modify the master page as you wish. Usually, I creates the table with few columns and rows (2 X 3). Merge the first 2 cells in the first row to use as header. Then merge 2 cells in the last row to use as footer. Middle row left cell use for menu and then middle row right cell use for the content. Cut and past the content place holder to here.

Combined pages have two Page_Load events one for master page ‘Page_Load’ event and another one for content page ‘Page_Load’ event. Content page ‘Page_Load’ event will fire before Master page Page_load event.

Combined page directive :

<%@PageLanguage=”C#”MasterPageFile=”~/MasterPage.master”Title=”First Content Page” %>

<asp:ContentID=”Content1″ContentPlaceHolderID=”ContentPlaceHolder1″Runat=”Server”>

</asp:Content>

  • Just do it manually remove all default tags from <HTML> to </HTML> then add necessary page directive for the master page file (MasterPageFile=). Then added the content server control using <asp:ContentID=”Content1″ContentPlaceHolderID=”ContentPlaceHolder1″Runat=”Server”></asp:Content>

  • Similar manner we can create the nested master page. But careful about this, visual studio 2008 and 2010 support this feature. Prior version display to edit manually. Once create the sub master page remove all the HTML contents from it then add page directive for the primary master page file (MasterPageFile=). Then add the content server control using <asp:ContentID=”Content1″ContentPlaceHolderID=”ContentPlaceHolder1″Runat=”Server”></asp:Content> then implement the content place holder inside the content.

// Code for visual web developer 2005

<%@MasterMasterPageFile=”~/MasterPage.master”Language=”C#” %>

<scriptrunat=”server”>

protectedvoid Page_Load(object sender, EventArgs e)

{

Session[“PageLoadOrder”] = Convert.ToInt32(Session[“PageLoadOrder”].ToString())+ 1;

Response.Write(“Sub Master “ + Session[“PageLoadOrder”].ToString() + “<br>”);

}

</script>

<asp:ContentID=”Content1″ContentPlaceHolderID=”ContentPlaceHolder1″Runat=”Server”>

Place the sub master inside the primary master content place holder

<asp:contentplaceholderid=”ContentPlaceHolder2″runat=”server”></asp:contentplaceholder>

</asp:Content>

Visual studio 2010 auto generated code for nested master page little bit different

<%@MasterLanguage=”C#”MasterPageFile=”~/Site1.Master”AutoEventWireup=”true”CodeBehind=”NestedMasterPage1.master.cs”Inherits=”MasterPage.NestedMasterPage1″ %>

<asp:ContentID=”Content1″ContentPlaceHolderID=”head”runat=”server”>

</asp:Content>

<asp:ContentID=”Content2″ContentPlaceHolderID=”ContentPlaceHolder1″runat=”server”>

</asp:Content>

When using nested master page Page_Load events are fired with following order.

  1. Content page Page_Load event

  2. Sub Master page Page_Load event

  3. Master page Page_Load event

Order of the eventes fired in the Combined page.

  1. Master page child control initialization

  2. Content page child control initialization

  3. Master page initialization

  4. Content page initialization

  5. Content page load

  6. Content page load complete

  7. Master page load

  8. Master page load complete

  9. Master page child control load

  10. Content page child control load

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s