DatePager分页问题

今天看了一下ListView和DataPager配合做数据分页的教程,感觉很爽很方便,用在自己的项目上面时却出现了问题,具体表现在点击上一页、下一页或者数字跳转页面时通常要点两下才能有反应,而且有时候乱跳页。 

我开始测试的代码是这样的:

public   partial   class  ListViewTest : System.Web.UI.Page
{
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        
if  ( ! Page.IsPostBack)
            BindData();
    }
    
    
protected   void  BindData()
    {
        DBDataContext db 
=   new  DBDataContext();
        var ds 
=  db.Category;
        ListView1.DataSource 
=  ds;
        ListView1.DataBind();
        db.Dispose();
    }
}

出现如开始提及的问题,找了半天原因也没有找到。后来在国外的一个论坛上找到了同病相怜的人,有专家给出了一个解决方案。把Page_Load里的数据绑定移到Page_PreRender中,也就是:

public   partial   class  ListViewTest : System.Web.UI.Page
{
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        
// if (!Page.IsPostBack)
        
//     BindData();
    }

    
protected   void  Page_PreRender( object  sender, EventArgs e)
    {
        BindData();
    }
    
    
protected   void  BindData()
    {
        DBDataContext db 
=   new  DBDataContext();
        var ds 
=  db.Category;
        ListView1.DataSource 
=  ds;
        ListView1.DataBind();
        db.Dispose();
    }
}

试了一下分页果然正常了。难道是Page_Load来的太迟?不得而知。另外,还有一种方法同样可行:

public   partial   class  ListViewTest : System.Web.UI.Page
{
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        
if  ( ! Page.IsPostBack)
            BindData();
    }

    
protected   void  Page_PreRender( object  sender, EventArgs e)
    {
        
// BindData();
    }

    
protected   void  ListView1_PagePropertiesChanging( object  sender, PagePropertiesChangingEventArgs e)
    {
        DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, 
false );
        BindData();
    }

    
protected   void  BindData()
    {
        DBDataContext db 
=   new  DBDataContext();
        var ds 
=  db.Category;
        ListView1.DataSource 
=  ds;
        ListView1.DataBind();
        db.Dispose();
    }
}

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值