views:

432

answers:

6

Have any ways to keep a variables (object) in a page scope? I need pass some object from master page to all ascx files. Normally, i passing by using a method or structure with parameters. But now im asking another way to do this. Store it in session is a bad idea because my object should exists in a page scope only.

A: 

If it's small, y not the query string?

Pierreten
+3  A: 

One way is to set up properties in your user controls. You have access to read and set these from all pages that implement them.

AaronS
If you define these properties in a base class or interface then they can form a contract between your user controls and master page.
cxfx
A: 

So you have masterpage, user controls and the page itself.

The page is the container for the masterpage and the user controls, hence it's the only one that 'knows' the 2 parties. So I'll suggest (if you haven't) you have the page instance to facilitate the object/variable passing amongst them.

o.k.w
+1  A: 

You could expose your variables a public properties of the master page:

public string MyVariable { get; set; }

then access them from the user controls by referencing the master page, and casting to its specific type:

((MyMasterPageType)Page.Master).MyVariable
cxfx
A: 

If your variable's value is going to be changed on per page basis then i would recommend you to write that code in base page (or user control) and inherit all the page (or usercontrol),if the values are going to be similar for all pages(or user control) you can use cache object as well.

In a more better approach if you feel you can even create one helper class and call it from your base page (or user control), so you can separate variable assignment code from your page.

Harryboy
+1  A: 

Another alternative is to store the shared object(s) in the HttpContext.Items collection.

Joe