How do I access a public page variable of the only page from the master page?
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?)
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?
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.
In your master page use this:
(this.Page as CMSPageViewer ).ID
Couldn't you use a session variable that you could access from both the page and the master page.
HttpContext.Current.Session[key] = value;
精彩评论