0

I have a page - only ONE page for the entire site:

using System;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class CMSPageViewer : System.Web.UI.Page
{
    public int myPageID;

    protected void Page_PreInit(object sender, EventArgs e)
    {
         // Get stuff from the database

         myPageID = 1;

         // Set the Page.MasterPageFile
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Fill in the page contents
        // This looks for and fills in asp:ContentPlaceHolder controls based on ID
    }
}

And the master page:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Standard_Page : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int myID = (this.Page as CMSPageViewer).myPageID;
    }
}

How can I access ID from the MasterPage that is used?

Casting the page doesn't seem to work. I get the following error:

Compiler Error Message: CS0246: The type or namespace name 'CMSPageViewer' could not be found (are you missing a using directive or an assembly reference?)
1
  • for this to work CMSPageViewer needs to be a "real" class (not a partial .aspx thingy) defined either in App_Code or in some Class Library. Jan 19, 2011 at 3:52

4 Answers 4

2

Define a variable in your master page. Give it proper getter/setters. In each page call the master.setter and "pass the variable into" the master page.

At the very least you're going about it wrong, but this is what you have to do.

Referencing "ASP.NET 3.5 Unleashed" http://www.amazon.com/dp/0672330113 section in Chapter 5 pages 255 - 260 for modifying Master properties based on the Page being interpreted.

Again, Referencing ASP.NET 3.5 Unleashed Chapter 6 which starts off:

Themes are different than Master Pages. A Master Page enables you to share content across multiple pages in a website. A Theme, on the other hand, enables you to control the appearance of the content.

So in other words, you're not supposed to use a Master Page as a Theme, the two work together.

Do you want me to keep going? I've got a few other books. I've done exactly what you're trying to do (modify the master based on the content) and at the time I knew it was a hack, but I did it because I had to. So what I'm telling you is what I did, which is what works.

Now please, again, tell me that what I'm suggesting can't be done. I'll go find some more references that will explain why you can't cast to a Page from within the Master.


For those looking to lay the smack down on my turning to a book for reference, note that that's how the big boys play, it's how the people who get the actual contracts play, and it's how you inform your boss. It's a book. That's what they're for. Additionally, Stephen Walther is not a nobody, he was a Senior Product Manager for the ASP.NET team. So he knows what the hell he's talking about. I would listen to his advice. Hell, I bought his book didn't I?

6
  • This is the opposite of what I'm asking. The master page is a theme. there could be 3-4 master pages on a site, each site has its on master pages. 99 times out of 100 the master page needs to know nothing about the page itself. there is no special requirement for the master page to work i would rather not require a method/variable exist on all possible master pages is they dont need it. That is why I'm asking... How can a master page access a variable from a page?
    – Justin808
    Jan 19, 2011 at 3:47
  • It's not the opposite of what you're asking, you're asking for a way for the master page to know what the inner page is doing. You can't assume all pages will have the same type (which you'll need to cast to if you're doing what @cybernate is suggesting) ... give me a few minutes to go dig up a textual reference (aka a book)
    – jcolebrand
    Jan 19, 2011 at 3:51
  • All you're master-pages should inherit from some base master-class which contains the logic to fetch this id. Then you can have a zillion different themes, just inheriting from this one base class so the logic doesn't get duplicated. Jan 19, 2011 at 3:53
  • +1 to this approach. I have used it in the past to adjust nav elements in a master page.
    – Jon P
    Jan 19, 2011 at 4:07
  • @drachenstern - I have not deleted any answers or comments. I have been reading them all.
    – Justin808
    Jan 19, 2011 at 4:19
2

I dislike the design, but if there is only a single page in the application, then this begins to make more sense.

In fact, this is not what master pages are for. This is what themes are for.

14
  • every page on my site used CMSPageViewer, its all filled in from the database. The master page is set in code and can be different depending on the page settings. The only stable item in the CMSPageViewer page. so if there is page ID that I need to access from the master page, I need to get it from the CMSPageViewer class.
    – Justin808
    Jan 19, 2011 at 3:15
  • @Justin: it still seems like a pretty goofy thing to do. Jan 19, 2011 at 3:19
  • 1
    @Justin808 - I agree that this isn't the best design. If it is a requirement of the master page that it know the Id, then the master page should do the work getting it, not expect the page to do it and then access it from there. This is the wrong way around. Jan 19, 2011 at 3:23
  • The master page is being used as a theme, putting all the work into the theme would be a little odd. Having the master page access a variable doesn't seems to outlandish to me.
    – Justin808
    Jan 19, 2011 at 3:26
  • 1
    If you're swapping master pages (which I've done before) it would be best to make them inherit from your own base master page that has common logic such as this requirement. Then make your master pages inherit from your base one, not System.Web.MasterPage. Jan 19, 2011 at 3:56
0

In your master page use this:

(this.Page as CMSPageViewer ).ID
7
  • Compiler Error Message: CS0246: The type or namespace name 'CMSPageViewer' could not be found (are you missing a using directive or an assembly reference?)
    – Justin808
    Jan 19, 2011 at 3:06
  • Add the full namespace of CMSPageViewer to the above. Jan 19, 2011 at 3:20
  • nothing is rapped in a namespace. what is the "default" namespace?
    – Justin808
    Jan 19, 2011 at 3:25
  • @Cybernate this will never work. Consider that a master page is a sort of wrapper for many pages. I see where you're going with this, but it won't work.
    – jcolebrand
    Jan 19, 2011 at 3:38
  • Am not saying whether it is right approach or not. I am just saying that technically it is possible.
    – Chandu
    Jan 19, 2011 at 3:40
-2

Couldn't you use a session variable that you could access from both the page and the master page.

HttpContext.Current.Session[key] = value;
2
  • thats a violation of the whole OO principle Jan 19, 2011 at 5:03
  • 1
    But, what's so OO about the whole situation here? At least its a cleaner way of accessing data, if not misused
    – Divi
    Jan 19, 2011 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.