Solved

Master Page from Project DLL

Posted on 2010-11-10
5
859 Views
Last Modified: 2013-11-26
Situation:
    Intranet setting.  End result will have about 12-14 separate projects for different departments, etc.  1 Project contains our login security classes, as well as a master page template for the look of the site.  This Master Page will be used for 98% of the site.

Now, the sunday drive version of doing this is to just copy the master page to every project, and hit the ground running.   However if a change ever needs to be done to the master page, we now are changing it in 12-14 projects.

So we tried outputting a dll of our main project that contains the master page, and then referencing that dll on the other projects.  This actually somewhat worked.  Except for the objects we have on the dll master page.  On that page we have simple linkbuttons for the navigation for our intranet.  When we test the dll project by itself, everything works fine.   But when we test the secondary project that pulls in the dll, the first object it comes to (in this case, a linkbutton), it gives an Object reference not set to an instance of an object error.

Easier explanation:  Project A contains security and Master Page Layout.  Project A is compiled into a dll.  Project B references dll.  Project B has a Master Page that Registers a TagPrefix from the DLL, and is attached onto the page, then Project B internal page references Project B's Master page.  I would be fine skipping Project B's master page if I could reference Project A's master page directly onto Project B's internal pages.

Any ideas?
Project B Master Page:



<%@ Register TagPrefix="cc1" Namespace="ProjA" Assembly="ProjA" %>



<body id="page1">

    <form id="form1" runat="server" style="height:100%">

    <cc1:SiteMaster ID="MasterForm" runat="server">

    

    </cc1:SiteMaster>

    <asp:ContentPlaceHolder ID="MainContent" runat="server">

        Test

    </asp:ContentPlaceHolder>    

</form>

</body>

Open in new window

0
Comment
Question by:hscast
  • 3
  • 2
5 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I believe the problem you are going to have is that when you compile web pages, only the code-behind actually gets put into a DLL; the markup pages are output just as they are, including master pages. You will want to build you master page DLL as you normally would, but you also want to be sure to place the master page's markup file somewhere where it can be referred to by your content page projects. I am including a sample solution to demonstrate. In the sample, I have modified the master page's markup file to be copied to the output directory during any build (via the Properties pane). Within the content page, I changed the MasterPageFile attribute of the content page to point to the master page markup file that now resides in the content page's "bin" folder, due to the aforementioned property change.
WebApplication3.zip
0
 

Author Comment

by:hscast
Comment Utility
Well, that really just does the same thing as adding the master page itself into each new project I create.

My new attempt involves my dll project outputting a placeholder, and building the markup langue in a stringbuilder and dynamically creating the controls, and then in my destination project calling that placeholder into my destination page's placeholder.

This works for the markup language and images.  It even works for creating the controls, as I can create a linkbutton in my dll project, and use it on my main project to response.redirect and the like.

However, what seems to be throwing a problem now, is that I cannot change the properties of the control.

For example, in my dll project, I have a page Build.cs  In that page I build what I want:

sb.Append(@"<div id=wrapper>");

            sb.Append(@"<div id=tail-top>");

            sb.Append(@"<div class=main>");

            sb.Append(@"<div id=header>");

            sb.Append(@"<div id=indent>");

            sb.Append(@"</div>");

            sb.Append(@"<a href=#>");



            Literal lit = new Literal();



            lit.Text = sb.ToString();



            Button Login = new Button();

            Login.ID = "Login";

            Login.Text="Login";

            Login.CssClass = "button-1";

            Login.Visible = true;



            

            plc.Controls.Add(lit);

            plc.Controls.Add(Login);



            Login.Click += new EventHandler(onLoginClick);



            Panel pnlLoggedIn = new Panel();

            pnlLoggedIn.Visible = false;

            pnlLoggedIn.CssClass = "button-1";



            Label UserName = new Label();

            UserName.ForeColor = System.Drawing.Color.White;

            pnlLoggedIn.Controls.Add(UserName);

            plc.Controls.Add(pnlLoggedIn);



            StringBuilder sb2 = new StringBuilder();

            sb2.Append(@"<div style=font-size: 24px; text-decoration: none; vertical-align: bottom; color: white; height:116px>");

            Literal lit2 = new Literal();

            lit2.Text = sb2.ToString();

            plc.Controls.Add(lit2);



            UpdatePanel uppnl = new UpdatePanel();

            uppnl.UpdateMode = UpdatePanelUpdateMode.Conditional;

            StringBuilder sb3 = new StringBuilder();

            sb3.Append(@"<div class=container><ul><li id=lihome>");

            Literal litup1 = new Literal();

            litup1.Text = sb3.ToString();

            uppnl.ContentTemplateContainer.Controls.Add(litup1);

            

            lnkHome.Text = "Home";

            lnkHome.Font.Bold = true;

            lnkHome.CssClass = "current";

            uppnl.ContentTemplateContainer.Controls.Add(lnkHome);

            lnkHome.Click += new EventHandler(lnkHome_Click);



            StringBuilder sb4 = new StringBuilder();

            sb4.Append(@"</li>");

            Literal litup2 = new Literal();

            litup2.Text = sb4.ToString();

            uppnl.ContentTemplateContainer.Controls.Add(litup2);



            StringBuilder sb5 = new StringBuilder();

            sb5.Append(@"<li id=liProduction>");

            Literal litup3 = new Literal();

            litup3.Text = sb5.ToString();



            lnkProduction.Text = "Production";

            lnkProduction.Font.Bold = true;

            uppnl.ContentTemplateContainer.Controls.Add(litup3);

            uppnl.ContentTemplateContainer.Controls.Add(lnkProduction);



            lnkProduction.Click += new EventHandler(lnkProduction_Click);



            StringBuilder sb6 = new StringBuilder();

            sb6.Append(@"</li></ul></div>");

            Literal litup4 = new Literal();

            uppnl.ContentTemplateContainer.Controls.Add(litup4);

            uppnl.ID = "uppnl";

            

            plc.Controls.Add(uppnl);



            sb.Append(@"</form>");

            sb.Append(@"</body>");

            return plc;



//After this code, I have a test handler:



 protected void lnkHome_Click(object sender, EventArgs e)

        {

            //LoadSubNav("Home");

            //lnkHome.ForeColor = System.Drawing.Color.White;

            LinkButton lnkHome = sender as LinkButton;

            lnkHome.ForeColor = System.Drawing.Color.White;

        }



Now, on my main page i have:



<asp:PlaceHolder ID="plc12" runat="server"></asp:PlaceHolder>



and in the code behind:



DllProject.Classes.Build Test1 = new Classes.Build();



            Literal lit1 = new Literal();

            plc12.Controls.Add(Test1.BuildHeader());



This puts everything in it's place, but whenever the code gets to the Forecolor change, I get a runtime error.

Open in new window

0
 

Author Comment

by:hscast
Comment Utility
Update:  I created a linkbutton dynamically OUTSIDE the dynamically created Update Panel, and I could change the properties just fine.

So it has something to do with messing with a dynamically created control within a dynamically created Update Panel.

Am I chasing a dead end here?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
>>  Well, that really just does the same thing as adding the master page itself into each new project I create.

How do you figure?
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
You do realize that I am talking about copying the master page markup page to one central location, and referring to it relatively from each content page, right? In this manner, you only need to change one file. This was demonstrated in my attached demo.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now