ListView + DataPager

来源:互联网 发布:淘宝上的天王表旗舰店 编辑:程序博客网 时间:2024/05/20 21:58

ListView、DataPager与LinqDataSource一起使用没问题,

不过当你不使用LinqDataSource的时候会发生一个小问题,就是当你在第一次点选DataPager

中的页码时并不会跟着跳页,而点选第二次的时候才会正常运作!所谓的「不使用LinqDataSource

的时候」是指你的资料来源的取得可能是写在Code Behind中或是使用ObjectDataSource或

SqlDataSource控制项,例如:所谓的「不使用LinqDataSource的时候」是指你的资料来源的取

得可能是写在Code Behind中或是使用ObjectDataSource或SqlDataSource控制项,例如:

protected void  Page_Load( object  sender, EventArgs e) protected void Page_Load( object sender, EventArgs e) 
 { 
     var q 
= from p in  db.News select p; var q = from p in db.News select p; 
     ListView2.DataSource 
= q; ListView2.DataSource = q; 
     ListView2.DataBind(); 
 } 

 

 

要解决这个问题必须要在ListView控制项宣告OnPagePropertiesChanging个事件,并执行该DataPager的
SetPageProperties方法,范例程式如下:要解决这个问题必须要在ListView控制项宣告
OnPagePropertiesChanging个事件,并执行该DataPager的SetPageProperties方法,范例程式如下:

protected void Page_Init( object sender, EventArgs e)

 {

     ListView2.PagePropertiesChanging += ListView2.PagePropertiesChanging += 

         new  EventHandler<PagePropertiesChangingEventArgs>(ListView2_PagePropertiesChanging); new EventHandler<PagePropertiesChangingEventArgs>(ListView2_PagePropertiesChanging);

 }

 

 void  ListView2_PagePropertiesChanging( object  sender, PagePropertiesChangingEventArgs e) void ListView2_PagePropertiesChanging( object sender, PagePropertiesChangingEventArgs e)

 {

     ListView srcListView = sender as  ListView; ListView srcListView = sender as ListView;

     this .DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false); this .DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

     srcListView.DataBind();

 }

 

转自http://www.cnblogs.com/kingjiong/archive/2008/10/16/1312395.html

原创粉丝点击