Solved

Master Page from Project DLL

Posted on 2010-11-10
5
863 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 75

Expert Comment

by:käµfm³d 👽
ID: 34102080
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
ID: 34144860
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
ID: 34145074
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 75

Expert Comment

by:käµfm³d 👽
ID: 34148530
>>  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 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34151919
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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