主页上没有 Page_PreLoad?

发布于 2024-10-18 21:51:37 字数 599 浏览 2 评论 0原文

我在会话中有一个自定义对象,它通过母版页和主页上的回发进行更新。

回发后,我需要获取 Session 对象,重建和修改它的部分或整个对象,然后将其加载回 Session。

我已经在主页的Page_Load中编写了这段代码。它仅在一页上运行良好。

现在我已经编写了具有相同母版页的另一个页面,并且我希望母版页能够像以前一样修改我的会话对象。

所以我想我只需要将会话处理代码移至母版页的 Page_Load 即可。但这并没有像我预期的那样工作,因为主页上的控件(即中继器)在母版页的 Page_Load 触发之前访问 OnItemDataBound 事件处理程序中的会话对象,这样它只能获取会话对象的先前状态。 (只有主页上的转发器是这样,母版页上的转发器在访问会话时获取当前状态)

无论我认为我可以使用母版页的Page_PreLoad事件,我都可以访问中的回发数据Page_PreLoad 也很好,并相应地更新会话对象,但我发现母版页上没有 Page_PreLoad,或者我无法使用它。

我应该在会话中的哪里更新我的对象?

总结一下:我需要在母版页的代码隐藏中放置一个位置,以便可以使用回发数据,并且主页和母版页的中继器的 OnItemDataBound 事件都尚未被触发。

I have a custom object in the Session which updates by postbacks on both the masterpage and the main page.

After the postback, I need to get the Session object, rebuild and modify parts of it or the whole object, and load it back into the Session.

I have written this code in the Page_Load of the main page. It works fine on only one page.

Now I have written an other page with the same masterpage, and I want the masterpage to be able to modify my session object just like before.

So I thought I just need to move the session handling code to the masterpage's Page_Load. But that did not work as I expected, as a control on the main page (namely a repeater) accesses the session object in the OnItemDataBound event's handler, BEFORE the masterpage's Page_Load fires, and this way it only gets the previous state of the session object. (It is only true for the repeater on the main page, the repeater on the masterpage gets the current state when it accesses the session)

No matter I thought I could use the Page_PreLoad event of the masterpage, I could access the postback data in the Page_PreLoad just as fine, and update the session object accordingly, but I found out there is NO Page_PreLoad on the masterpage, or I fail to use it.

Where should I update my object in the session?

To sum it up: I need a place in the masterpage's codebehind where postback data is ready to use, and neither of the main page's nor the masterpage's repeater's OnItemDataBound event have been fired yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦幻的味道 2024-10-25 21:51:37

还有另一种解决方案:
在母版页的 init 事件中,您实际上可以订阅页面的预加载事件。
在母版页中考虑以下代码:

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Page.PreLoad += OnPreLoad;
    }

    protected void OnPreLoad(object sender, EventArgs e)
    {
        //this function is in the masterpage but will be called on page preload event so do here your preload stuff ...
    }

There is also another sollution:
in the init event of the masterpage you can actually subscribe for the preload event of the page.
consider this code in the masterpage:

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Page.PreLoad += OnPreLoad;
    }

    protected void OnPreLoad(object sender, EventArgs e)
    {
        //this function is in the masterpage but will be called on page preload event so do here your preload stuff ...
    }
番薯 2024-10-25 21:51:37

希望我正确理解这一点 - 我认为最好的选择是创建一个基本页面,并使您的页面继承此页面。将您的逻辑放入基页的 Page_LoadPage_PreLoad 事件处理程序中。母版页在实际页面开始加载后加载。

因此,您创建一个基本页面:

public class BasePage : Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      // sesion logic here
   }  
}

并使您的页面继承此页面(以及使用您的母版页):

public class Page1 : BasePage //instead of Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      base.Page_Load(sender, e);
   }  
}

Hopefully I'm understanding this correctly - I think your best option is to create a base page, and make your pages inherit from this. Put your logic in the Page_Load or Page_PreLoad event handler in the base page. Master pages are loaded after the actual page starts to load.

So, you create a base page:

public class BasePage : Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      // sesion logic here
   }  
}

And make your page inherit from this (as well as using your master page):

public class Page1 : BasePage //instead of Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      base.Page_Load(sender, e);
   }  
}
清音悠歌 2024-10-25 21:51:37

来自 asp.net 论坛

“页面”是一个用户控件,由实际页面托管>。这意味着它们仅经历控件生命周期,而不是页面生命周期。

正如建议的,最好的方法是使用基本的 Page 类,这样您就可以在页面之间共享通用代码。另一种方法是使用 Application 类支持的 Request 事件之一。

我最终只是使用了 Init 事件(因为没有 PreLoad 事件):

protected void Page_Init(object sender, EventArgs e)
{
   if (Request.Params["debug"] != null)
      Page.Trace.IsEnabled = true;
}

From asp.net forums:

Master "pages" are a UserControl, hosted by the actual Page. This means they only go through a control lifecycle, not the page lifecycle.

As suggested the best approach is a base Page class, so you can have common code shared among pages. Another is to use one of the Request events supported by the Application class.

i ended up simply using the Init event (since there is no PreLoad event):

protected void Page_Init(object sender, EventArgs e)
{
   if (Request.Params["debug"] != null)
      Page.Trace.IsEnabled = true;
}
爱已欠费 2024-10-25 21:51:37

如果您正在寻找此功能,您还可以覆盖预加载:

public class BasePage : System.Web.UI.Page
{
    public BasePage()
    {
    }
    protected override void OnPreInit(EventArgs e)
    {
        MasterPageFile = "MasterPage2.master";

    }  
}

you can also override preload if you are looking for this:

public class BasePage : System.Web.UI.Page
{
    public BasePage()
    {
    }
    protected override void OnPreInit(EventArgs e)
    {
        MasterPageFile = "MasterPage2.master";

    }  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文