开发者

declaring a session dictionary in a master page

开发者 https://www.devze.com 2023-02-07 05:08 出处:网络
How do you declare a dictionary called MyDic in the master page? I want MyDic to hold lists of objects MyObj with a date as the key so that I can write something like this: \"get the list for date 1/

How do you declare a dictionary called MyDic in the master page?

I want MyDic to hold lists of objects MyObj with a date as the key so that I can write something like this: "get the list for date 1/28/2011 from MyDic" or "put this list of MyObj from 1/28/2011 in MyDic".开发者_如何学Python

I'd like to declare the dictionary in the master page so that I can access it in every page.

Thanks.


How were you planning on persisting the list? Instead of creating it in the master page, you could create it as an application variable, or if it is user-specific as a session variable. In either case you would do something like:

Dictionary<DateTime, myObj> myList = new Dictionary<DateTime, myObj>();

Then you would store it somewhere:

Session["MyList"] = myList;

and when you need to access it again:

Dictionary<DateTime, myObj> myList = (Dictionary<DateTime, myObj>)Session["MyList"];

You could do the declaration in the Master Page on init or on load, or even better i would suggest doing it in the global.asax file for either session or application


You can create a public property on Master page and then use it in content pages by setting MasterType Directive. e.g. if you have a Master Page named MyMasterPage then here is the code sample

/// declare it in Master Page
public Dictionary<DateTime, typeof(List<MyObj_Type>)> MyDic {
      get;
      set;
}

/// put the line just under Page directive on your content page where you want to access it

<%@ MasterType  virtualPath="~/MyMasterPage.master"%>

/// and then on content page you can access by
** Note: The intelisense may not work but don't worry just put the code in content page and it works.**

Master.MyDic.Add(DateTime.Now, MyObj);

Here is details how Accessing Members on the Master Page

Have a good day!!!


You can use a session or application variable for this kind of sharing.

This kind of scenario is pretty much what they were created for.

0

精彩评论

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