导航离开 Gridview 页面后维护 GridView 当前页面索引 [英] Maintaining GridView current page index after navigating away from Gridview page

查看:22
本文介绍了导航离开 Gridview 页面后维护 GridView 当前页面索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ASP.NET Web 表单上有一个 GridView,我已将其绑定到数据源并将其设置为每页 10 条记录.

I have a GridView on ASP.NET web form which I have bound to a data source and set it to have 10 records per page.

我在 GridView 上还有一个超链接列,以便用户可以从列表导航到另一个页面(详细信息页面).在详细信息页面上,他们有返回"按钮可以返回到 GridView 页面

I also have a hyper link column on the GridView, such that a user can navigate to another page (details page) from the list. On the details page, they have "Back" button to return to the GridView page

编辑
只是为了澄清查询

Edit
Just to clarify the query

我正在寻找服务器端的示例代码片段,了解如何在数据绑定后指定页面索引以设置 GridView.这个想法是为了确保用户导航到他们所在的相同页面索引.

I am looking for sample code snippet on the Server Side on how to specify the page index to set the GridView after data binding. The idea is to ensure the user navigates to the same page index they were on.

推荐答案

您可以使用的三个基本选项:查询字符串、会话、cookie.它们各有优缺点:

The three basic options at your disposal: query string, session, cookie. They each have their drawbacks and pluses:

  1. 使用查询字符串将要求您使用 gridview 格式化所有指向页面的链接,以便在查询字符串中包含正确的信息(最终可能不仅仅是页码).
  2. 如果您确定每个浏览器实例都想要转到同一个 gridview,则使用会话会起作用,否则您必须使用某些 id 键标记会话变量,该 id 键对于相关的每个 gridview 页面都是唯一可识别的.这可能会导致对许多完全不受欢迎的变量进行会话管理,因为它们中的大多数只能在超时后过期.
  3. 使用 cookie 需要类似的东西,其中 cookie 数据存储在键/数据矩阵中(优化的哈希表可能适用于此).不建议为您正在跟踪的每个 gridview 页面使用单独的 cookie 名称,而是使用具有通用名称的 cookie 来保存所有被跟踪的 gridview 页面的数据,并在其中具有键/值结构.

一个关于设置页面索引的小代码片段.

A small code snippet on setting the page index.

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        try
        {
            if(HttpContext.Current.Request["myGVPageId"] != null])
            {
                myGridview.PageIndex = Convert.ToInt32(HttpContext.Current.Request["myGVPageId"]);
            }
        }
        catch(Exception ex)
        {
            // log it
        }
    }
}

这篇关于导航离开 Gridview 页面后维护 GridView 当前页面索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆