GridView分页后跳转其他页面进行添加,编辑,删除操作后,仍返回到当前页码[原]

1.App_CODE部分SqlHelper类添加一全局静态变量gdPageIndex.
    //记录GridView分页信息,全局静态变量
    public static string gdPageIndex = "0";
2.主页面CodeBehind代码,前台页面很简单,一个GridView控件而已,代码省略.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Main : System.Web.UI.Page
{
    
//数据添加,编辑,删除页面简称维护页.
    private string strSql = string.Empty;

    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            
this.bind(); //给GridView绑定数据.
        }

    }


    
protected void bind()
    
{
        strSql 
= "SELECT ID,MNTH,STAT,L12,RPT,DRILL FROM dbo.STATINFO ORDER BY MNTH DESC";
        DataSet ds 
= SqlHelper.GetDataSet(strSql);

        
this.grvMain.DataSource = ds;
        
this.grvMain.DataKeyNames = new string[] "ID" };
        
//Session["gvPageIndex"]的值在维护页设置.
        if (Session["gvPageIndex"== null)
        
{
            
//Session["gvPageIndex"]为null,即普通的第一次加载页面,不是从维护页返回的.
            if (!Page.IsPostBack)
             {
                  SqlHelper.gdPageIndex = "0";
                
this.grvMain.PageIndex = 0//普通加载,加载第一页数据.
             }
          }
        else if (Session["gvPageIndex"== "True")
        
{
            
//Session["gvPageIndex"]为True,即是从维护页返回的,True值在维护页设置.
            if (!Page.IsPostBack)
                
this.grvMain.PageIndex = Convert.ToInt32(SqlHelper.gdPageIndex); //调用保存在全局变量里的PageIndex值.
           
Session["gvPageIndex"] = null//此处Session值要清空,不然刷新页面就不会在第一页.
        }

        
this.grvMain.DataBind();
    }


    
protected void grvMain_PageIndexChanging(object sender, GridViewPageEventArgs e)
    
{
        
this.grvMain.PageIndex = e.NewPageIndex;
        SqlHelper.gdPageIndex 
= e.NewPageIndex.ToString(); //将当前PageIndex的值保存进全局变量gdPageIndex.
        this.bind();
    }

}
3.数据维护页面.前台代码省略,一数据更新按钮btnUpdate.下面是CodeBehind代码
    protected void btnUpdate_Click(object sender, EventArgs e)
    
{
        Session[
"gvPageIndex"= "True"//设置Session["gvPageIndex"]为True
        Response.Redirect("Main.aspx"); //转回主页面
    }
OK!

posted @ 2008-02-21 16:53  江湖工夫  阅读(3566)  评论(4编辑  收藏  举报