views:

541

answers:

4

I am building pages dynamically using a database to store the page info and .NET (C#) to build pages. Part of that process is to set the masterpage (in the code-behind) based on what is in the database and, as I understand it, that has to be done in Page_PreInit.

My problem is how to pass objects and variables from Page_PreInit to Page_Load.

I have been able to make it work as follows, but am having random compiling errors when using this method:

public partial class BuildPage : System.Web.UI.Page
{
    protected static string pageData;

    protected void Page_PreInit(object sender, EventArgs e)
    {
        --- SET pageData = DATA FROM DATABASE, AND SET MASTERPAGE ---
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        --- USE pageData TO BUILD AND DISPLAY THE REST OF THE PAGE ---
    }
}

For various reasons, I'm am not using Visual Studio to compile the page, just letting .NET compile on the fly at the first page request. I have gotten two error messages:

1) "CS0102: The type 'BuildPage' already contains a definition for 'pageData'"

2) "ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl)."

The strange thing is that sometimes the page compiles on the first web request. And, for those times that it doesn't on the first request, after a random number of page refreshes, it will compile perfectly. After it compiles everything appears to work fine until I make another change to the code behind and it has to recompile.

I seem to only get those compiling errors when using that method to share variables between Page_PreInit and Page_Load. In other words, if I simply request the data twice from the database, once in 'Page_PreInit' and once in 'Page_Load' I never get those errors. But I really would rather not double the database load.

So my question really has two parts, first, is that an acceptable way to share variables or is there a better way to pass data from Page_PreInit to Page_Load?

And second, if that is an acceptable way, are the errors unrelated and has anyone seen similar errors that simply go away after repeated web requests before?

Thanks for any and all help! fodder

A: 

You most likely don't want the member variable to be static. It could produce strange results if more than one person is accessing the page at the same time.
It might also be the direct cause of your problem because of the way templates work, though I'm not certain of that.

Thorarin
A: 

Using a protected (or private) member is definitely the correct way to share objects between methods in a class. However, your member should not be static. Static means there is a single instance across multiple threads. Every copy of the page which is being executed for the different requests accessing that page are competing to read/write that member.

Also, your class is marked "partial". That means there could be another class fragment in another file which has additional members and methods for the same BuildPage class. If you declare the same pageData member in both, they will conflict.

Rex M
Ah, thanks so much, the "static" modifier was exactly the problem.I'm pretty sure that "partial" class is correct (http://msdn.microsoft.com/en-us/library/system.web.ui.page.aspx) but I will definitely do more investigation into that as well.Thanks for the quick and complete response!
@fodder great! To be clear - there is nothing necessarily wrong with "partial" - it's just something to keep in mind that when something is marked "partial", there *can* be duplicate member declarations elsewhere.
Rex M
A: 

In your codebehinds, do you have multiple pages with the class specified as BuildPage? They should be unique for each page and referenced properly in the page directive of the associated .aspx. I think the CS0102 error is because you have two partial classes with the same name, both defining a static value. So when the two partials are combined, you're trying to define the static twice in the combined class. If you get your class names/page directives sorted out, that should solve the second error too.

Secondly, there's no reason for the pageData variable to be static. You will be able to set and access the value without it being static.

John Sheehan
A: 

Static won't be what you want - that is shared by all instances of the page, so if two clients are loading the same page, the second would over write the first, with the possibility of doing it between the init and load of the first.

You could use the HttpContext for the page to pass data around, it flows through all the events.

blowdart