简体   繁体   English

ASP.NET如何使用跨页回发引发上一页的加载事件

[英]ASP.NET How to raise load event on previous page with cross page postbacks

I'm working on a wizard-like set of page, and I'm relying on cross page postbacks to navigate between them. 我正在处理一组类似于向导的页面,并且依靠跨页面回发在它们之间进行导航。

I need to be able to trigger the Load event on the previous page in order to save the form data for the page. 我需要能够触发上一页的Load事件,以保存该页面的表单数据。

I've been told that for situations of this sort all I had to do is access the PreviousPage property in the destination page and this would trigger the load event of the previous page but for some reason this doesn't seem to be working. 有人告诉我,对于这种情况,我要做的就是访问目标页面中的PreviousPage属性,这将触发上一页的load事件,但是由于某种原因,这似乎不起作用。

Is there anything else I can do to explicitly trigger the load event on the previouspage if the PreviousPage property is not null? 如果PreviousPage属性不为null,我还能做些什么来显式触发上一页的加载事件?

Thanks for your help, 谢谢你的帮助,

Yong

Have you considered moving whatever persistence logic you're doing in the load of the Previous Page into a method on the page? 您是否考虑过将上一页加载中正在执行的任何持久性逻辑转移到页面上的方法中?

That way you can just hit: 这样,您可以点击:

if(PreviousPage != null)
   PreviousPage.DoThatSavingThing();

Obviously you'd need to type it to get the specific methods you add unless you added those to all pages. 显然,除非您将所有方法都添加到所有页面中,否则需要键入它来获取添加的特定方法。

This sounds a little confusing to me, but if you are wanting access to data, it's best to save that data into something that you can easily get to, like the ASP.NET session cache. 这听起来让我有些困惑,但是如果您想访问数据,最好将数据保存到您可以轻松访问的内容中,例如ASP.NET会话缓存。 So instead of going back to a previously navigated page in order to get data, you will cache the data the first time you reach the first page, and then when the user navigates to the 2nd page, it will have access to that information. 因此,您无需回到先前导航的页面以获取数据,而是在第一次访问第一页时缓存数据,然后当用户导航到第二页时,它将可以访问该信息。

To add - I tested using two methods of getting a "strongly-typed" previouspage. 补充-我使用两种方法测试了获得“强类型”上一页的方法。

  1. Added a reference to the destination: 添加了对目的地的引用:

  2. Added the PreviousPage directive on the destination: 在目标上添加了PreviousPage指令:

When accessing the PreviousPage property in the destination, the Load event was fired on the PreviousPageName page (source). 访问目标中的PreviousPage属性时,在PreviousPageName页(源)上触发了Load事件。

Example (assuming there is a public property named Test on the PreviousPageName (Source page)): 示例(假设在PreviousPageName(“源”页面)上有一个名为Test的公共属性):

protected void Page_Load(object sender, EventArgs e)
{
    if (PreviousPage != null)
    {
        //Using a reference, you have to cast:
        PreviousPageName x = (PreviousPageName)PreviousPage;
        string test = x.Test;

        //Using the PreviousPage directive, you do not need to cast:
        string test2 = PreviousPage.Test


    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM