当页面分页时,点击下一页页面就会重新执行一次page_Load事件。当我们从很多数据中只选择某种类型的数据时,当选择的数据不只一页时,经常会出现这样一种现象,点击下一页时,页面有跳转到初始页面。那时因为当你点击下一页是,页面重新刷新,上一次传递的参数变为null的原因。解决方法:只需要在page_Load中做一下判断即可。
例如:
  public partial class members : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["aa"] == null)
                {
                    Session["aa"] = "yes";
                    Session["Member"] = Request["stuID"];
                }
                else
                {
                    if (Request["stuID"] == null)
                    {
                        Session["Member"] = Session["Member"];
                    }
                    else
                    {
                        Session["Member"] = Request["stuID"];
                    }
                }
                memberBind();
                TeamBind();
            }

        }

        private void memberBind()
        {
            int memStuID = Convert.ToInt32(Session["Member"]);
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = MembersBll.GetMembers(memStuID).DefaultView;
            pds.AllowPaging = true;
            pds.PageSize = 4;
            int CurPage;
            if (Request.QueryString["page"] != null)
            {
                CurPage = Convert.ToInt32(Request.QueryString["page"]);
            }
            else
            {
                CurPage = 1;
            }
            pds.CurrentPageIndex = CurPage - 1;
            int Count = pds.PageCount;
            LabCount.Text = "共[" + Count.ToString() + "</font>]页";
            LbPage.Text = "第[" + CurPage.ToString() + "]页";
            if (!pds.IsFirstPage)
            {
                lnkPrev.ForeColor = System.Drawing.Color.Blue;
                lnkPrev.Enabled = true;
                lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurPage - 1);
            }
            if (!pds.IsLastPage)
            {
                lnkNext.ForeColor = System.Drawing.Color.Blue;
                lnkNext.Enabled = true;
                lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurPage + 1);
            }
            RepMember.DataSource = pds;
            RepMember.DataBind();
        }

        private void TeamBind()
        {
            int memStuID = Convert.ToInt32(Session["Member"]);
            DataTable dt = MembersBll.GetMembers(memStuID);
            foreach (DataRow dr in dt.Rows)
            {
                TeamName.Text = dr["stuName"].ToString();
            }
        }
    }

posted on 2013-06-18 13:50  零点HY  阅读(226)  评论(0编辑  收藏  举报