如何访问当前[网页]页面(当前页面之外)?

时间:2009-10-12 18:10:58

标签: c# asp.net master-pages

情况如下:

我有一个全局使用的抽象类,用于通过公共属性引用Session变量。我们现在想要更改其行为以返回母版页上的属性。

(通过简单地更改此特定属性的内容,我们希望避免进行大量重写)

这只是该课程的片段:

   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");
            }
        }
    }

上面,“CurrentPage”不是真实/有效的。我只是写那里显示上下文。

这甚至可能吗?

谢谢! Ĵ

3 个答案:

答案 0 :(得分:2)

查看HttpContext.Current对象。我相信它的Handler属性将返回当前正在执行的页面。读取存储在会话中的值会更容易将其拉出属性,因为会话可以从HttpContext.Current开始。

答案 1 :(得分:1)

David's answer上构建,可以在整个应用程序中静态使用:

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

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

答案 2 :(得分:0)

我认为你需要使用带有“页面”对象作为参数的东西,然后你可以确定传递的页面是否是你的母版页。并从那里做你需要的......

但这增加了相当多的开销。

这里真正的问题是你想避免什么?试图摆脱会话并转移到viewstate?

相关问题