开发者

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 fil开发者_运维技巧es. 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.


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.


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


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 


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜