complez
complez

Reputation: 8352

asp.net - variable pass between ascx and page

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.

Upvotes: 0

Views: 2716

Answers (5)

to StackOverflow
to StackOverflow

Reputation: 124804

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

Upvotes: 1

Harishbb
Harishbb

Reputation: 1114

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.

Upvotes: 0

Chris Fulstow
Chris Fulstow

Reputation: 41902

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 

Upvotes: 1

o.k.w
o.k.w

Reputation: 25820

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.

Upvotes: 0

AaronS
AaronS

Reputation: 7713

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.

Upvotes: 3

Related Questions