frenchie
frenchie

Reputation: 51947

declaring a session dictionary in a master page

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".

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

Thanks.

Upvotes: 1

Views: 1097

Answers (3)

Waqas Raja
Waqas Raja

Reputation: 10870

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!!!

Upvotes: 2

Victor
Victor

Reputation: 4721

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

Upvotes: 2

Oded
Oded

Reputation: 499062

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.

Upvotes: 0

Related Questions