Mukarram Mukhtar

Share Master Page (p 3)

Shared Master Page through Precompiled Re-Usable Code in .NET

The theory behind this approach is quite simple and similar to the approach using virtual directories. You create a web site and add a Master page in it, publish the site using IDE’s publish tool and make such settings that your published site is precompiled and updatable; plus make single page assemblies. Now create another web application which will be using and sharing master page with other similar applications, call this client application. In the client application, add reference to the single page assemblies that were created during the publish process and also add the master page file in the client project; master page file will be updatable in the client project.

Just like previous approaches, this theory is strong and persuasive too but with similar practicle complications we saw in first two parts. Let’s see how we deal with them and try to find workarounds.

Creation of Master Page Website:

Let’s do it step-by-step:

  • Start Microsoft Visual Studio 2005 or later.
  • Click File->New->Website menu
  • In New Website dialog box, select Visual C# in language, File system in location.
  • Select ASP.NET Web site in the Templates panel on top.
  • Give a decent physical path and name to your website and select OK.

SMPCreateWebsite

  • Right click the website in the solution explorer and add a Master page in the project.
  • Delete all other default web pages, web.config files, app_code and app_data folders from the project.
  • Add a new folder named images and add an image in it, I added Fixfox_Rox.png

After completing the above steps your solution explorer should look something like this:

SMPSolutionExplorer

Next step is to add some simple code in Master page; this can be done by copying the following code in the master page:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Main.master.cs" Inherits="Main" %>
<!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>Untitled Page</title>   
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Main Master Header</h1>
        <asp:Image ID="imgFirefox" ImageUrl="~/images/Firefox_Rox.png" runat="server" Width="100px" />
        <h3><asp:Label ID="lblDate" runat="server"></asp:Label></h3>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
        <h3>Main Master Footer</h3>
    </div>
    </form>
</body>
</html>

In the Main.master.cs, add one line of server side script as follows:

protected void Page_Load(object sender, EventArgs e)
    {
        this.lblDate.Text = DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToLongTimeString();
    }

Compile the website to check for any compile time errors. If everything is good then next step is to publish our website. Right click the website in the solution explorer and select Publish Web Site in the popup menu. In the Publish website dialog box, enter a decent path to publish and select top two options as follows:

SMPPublish

The website will be published and published contents will look like these:

SMPPublishedWebsite

And bin folder contains a DLL:

D:\My Work\ShareMasterPrecompile\ShareMasterOutput\bin\App_Web_main.master.cdcab7d2.dll

Well, our master page website is ready to be shared, now let’s quickly create master page client application.

Creation of Master Page Client Application:

  • Start Microsoft Visual Studio 2005 or later.
  • Click Create New Project
  • In New Project dialog box, select Visual C# in Project types panel on left.
  • Select ASP.NET Web Application in the Templates panel on right.
  • Give a decent physical path and name to your project and select OK.

SMCPCreateProject

  • IDE will create a web project for you with a Default.aspx web page in it, delete the Default web page.
  • Right click on the References and add a reference of App_Web_main.master.cdcab7d2.dll from the path given above
  • Right click on the project in solution explorer and select Add->Existing Item in the popup menu; select Main.master from the ShareMasterOuput directory.
  • Add a new folder in the project, name it images; copy the image from ShareMasterOuput\images into this folder.
  • Right click on the Main.master file and select Add content page in the popup menu.

After completing the above steps, your solution explorer should look something like this:

SMCPSolutionExplorer

WebForm1.aspx’s code should look as follows:

<%@ Page Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="prjShareMasterClientPrecompile.WebForm1" Title="Share Master Page Using Precompiled Code" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    Hello, I'm a content web page.
</asp:Content>

Well, our client application is ready to roll, so let’s run the application and see how it is working:

SMCPCompleteRun

Pros and Cons:

  • We can share a master page by creating a website which has master page, images, css and javascript files; and publish this website using certain options; and finally adding proper references into the client web application.
  • Unlike in Virtual Directory approach, in this approach client project’s content web pages do not lose Design view mode
  • If the master page has some images, css files and javascript files then these files have to be copied in all the client web applications
  • If the master page has some server side script then master page project’s dll has to be added in references of all the client web applications.
  • Every time we make any change in HTML of the master page, it will not be automatically reflected in the client web application and references in all the client applications will have to be updated.
  • Every time there is a change in images, css or javascript files, these files have to copied in all the client web applications
  • Every time there is a change in server side script of the master page, new dll does not need to be copied in all the client web applications, because all the client projects are referencing the dll from a common shared location where master project was published.

Well, that’s it; that’s the end of sharing master page using precompiled code.

1 Comment »

  1. I don’t even understand how I stopped up right here, but I assumed this publish used to be good. I don’t understand who you’re however definitely you are going to a famous blogger when you are not already. Cheers!

    Comment by מבחן אישיות — May 2, 2013 @ 10:17 pm


RSS feed for comments on this post. TrackBack URI

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

Create a free website or blog at WordPress.com.

%d bloggers like this: