如何在请求之间存储 ASP.NET WebForms 页面状态

发布于 2024-12-19 06:26:30 字数 978 浏览 0 评论 0原文

首先,这就是我想要实现的目标:

想象一个带有 GridView 和其他一些控件的 ASP.NET 页面。用户对 GridView 应用一些分页、排序和过滤,然后单击链接并转到另一个页面。他或她最终可以浏览更多页面,然后返回到 GridView 页面。然后,当她离开页面时,她会看到具有相同分页/排序/过滤的网格。

我可以轻松地在一页上执行此操作,但我需要将此行为应用到数十个页面。

我想要的效果是:

String PageStateKey {
  get { return "PageState_" + Page.UniqueID; }
}

void Page_PreRender() {
    // this is executed in every postback, and saves page state for future requests
    var pageState = GetCurrentPageState(); // gets an object with ViewState, query string, and form values
    Session[PageStateKey] = pageState; // persist page state to session
}

void Page_Init() {
    // this restores a page state saved in a previous request, if any
    var restoredPageState = Session[PageStateKey];
    if (restoredPageState != null) {
       ApplySavedStateToPage(restoredPageState);
    }
}

然后我的页面可以从 StatePage 或类似的东西继承。我找到了一些解决方案,但它们需要更改我的代码(即:使用自定义方法来重定向页面而不是 Response.Redirect 等)

提前致谢

First, this is what I want to achieve:

Imagine an ASP.NET page with a GridView and some other controle. The user applies some paging, sorting and filtering to the GridView, then clicks a link and goes to another page. He or she can eventually wander through some more pages, then come back to the GridView page. Then she sees the Grid with the same paging/sorting/filtering when she left the page.

I could do this easily to one page, but I need to apply this behavior to dozens of pages.

I want something to the effect of:

String PageStateKey {
  get { return "PageState_" + Page.UniqueID; }
}

void Page_PreRender() {
    // this is executed in every postback, and saves page state for future requests
    var pageState = GetCurrentPageState(); // gets an object with ViewState, query string, and form values
    Session[PageStateKey] = pageState; // persist page state to session
}

void Page_Init() {
    // this restores a page state saved in a previous request, if any
    var restoredPageState = Session[PageStateKey];
    if (restoredPageState != null) {
       ApplySavedStateToPage(restoredPageState);
    }
}

Then my pages could inherit from a StatePage or something like this. I found some solutions but they require changing my code (i.e: using a custom method to redirect pages instead of Response.Redirect, etc.)

Thanks in advance

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

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

发布评论

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

评论(1

脱离于你 2024-12-26 06:26:30

我能够使用以下解决方案解决它:

http://www.codeproject.com/KB/applications/persistentstatepage.aspx?fid=73573&df=90&mpp=25&noise=3&prof=False&sort=Position&view= Quick&fr=26

我只做了两处修改:

1) 取消注释第 39 行以使其与 ASP.NET 一起使用4:

// EnableEventValidation=false;

2) 将 SavePageStateToPercientMedium 更改为始终调用 SavePageState(),无论是否是重定向

I was able to solve it using this solution:

http://www.codeproject.com/KB/applications/persistentstatepage.aspx?fid=73573&df=90&mpp=25&noise=3&prof=False&sort=Position&view=Quick&fr=26

I only did two modifications:

1) Uncommented line 39 to make it work with ASP.NET 4:

// EnableEventValidation=false;

2) Changed SavePageStateToPersistentMedium to always call SavePageState() regardless it is a redirect or not

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