views:

58

answers:

3

The situation is this:

I have an abstract class used globally that used to reference a Session variable via a public property. We now want to change its behavior to return a property on a master page.

(By simply changing the guts of this particular property, we hope to avoid doing a lot of rewrites)

This is just a snippet of the class:

   public abstract class AppSession
   {
        public static CaseNumber CurrentCaseNo
        {
            /* OLD METHOD DELETED */

            get
            {
                if (CurrentPage.Master != null)
                 // property on the master page
                  return CurrentPage.Master.CurrentCaseNo;
                else
                  throw new Exception("This page has no master page");
            }
        }
    }

Above, "CurrentPage" is not real/valid. I just wrote that there to show context.

Is this even possible?

Thanks! J

A: 

I think that you would need to work with something that took a "page" object in as a parameter, and from there, you could determine if the page that was passed is your master page. And do what you need from there....

But that adds quite a bit of overhead.

The real question here is what are you trying to avoid? Trying to get rid of session and move to viewstate?

Mitchel Sellers
Yes, the alternative is to pass in the Page as a parameter. We had considered that and it's our last choice. Yes, we are trying to get away from the session variable. Thanks.
Marc
+2  A: 

Look at the HttpContext.Current object. I believe it's Handler property will return the currently executing page. It would be easier to read a value stored in the Session that pulling it out of a property since the Session is available off of HttpContext.Current.

David
+1  A: 

Building on David's answer, this can be used statically throughout your application:

Page myPage = System.Web.HttpContext.Current.CurrentHandler as Page;

if( myPage != null )
    return ((MyMaster)myPage.Master).CurrentCaseNo;
Greg