GridView自定义分页

有时候的只是需要一些简单的但却是自定义的分页功能,但是又舍不得objectdatasource的排序功能,那就只有把pageddatasoure和objectdatasour结合起来,

由于pageddatasource实现的是IEnumberable,直接把objectdatasource赋给它是不行的,但是它的select()方法,却正好可以如愿:

  
    
1 ods.SelectParameters[ " strWhere " ].DefaultValue = " 1=1 " ;
2
3 PagedDataSource ps = new PagedDataSource();
4   int currentPage = int .Parse(txtcurrentPage.Text);
5 ps.DataSource = ods.Select();

 

更新:

以上误,一旦gridview的数据源不再是objectdatasource,sorting功能也随即失效,而且还会报错,因为没有响应onsorting方法,我去编写sort方法的时候,却发现gridview的sortExpression和sortDirection都是只读的,也就是说,不能通过简单的只改属性的方法让其顺利排序,那么,就得用代码手动排序了,这样还有什么用gridview的方便所在?放弃吧。。

你有什么折衷的方法呢?

 

解决:

既然要求的分页不是太复杂,就用了<PagerTamplate>自定义分页,这样排序的问题就不需要考虑了。

并于此处找到相应方法:

当前页

  
    
1 < asp:Label ID = " LabelCurrentPage " runat = " server "
2 Text = " <%# ((GridView)Container.NamingContainer).PageIndex + 1 %> " ></ asp:Label >

总页数

  
    
1 < asp:Label ID = " LabelPageCount " runat = " server "
2 Text = " <%# ((GridView)Container.NamingContainer).PageCount %> " ></ asp:Label >

首页,尾页,上一页,下一页

代码
   
     
1 < asp:LinkButton ID = " LinkButtonFirstPage " runat = " server " CommandArgument = " First " CommandName = " Page "
2 Visible = " <%# ((GridView)Container.NamingContainer).PageIndex != 0 %> " > 首页 </ asp:LinkButton >
3
4   < asp:LinkButton ID = " LinkButtonPreviousPage " runat = " server " CommandArgument = " Prev " CommandName = " Page "
5 Visible = " <%# ((GridView)Container.NamingContainer).PageIndex != 0 %> " > 上一页 </ asp:LinkButton >
6
7   < asp:LinkButton ID = " LinkButtonNextPage " runat = " server " CommandArgument = " Next " CommandName = " Page "
8 Visible = " <%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %> " > 下一页 </ asp:LinkButton >
9
10   < asp:LinkButton ID = " LinkButtonLastPage " runat = " server " CommandArgument = " Last " CommandName = " Page "
11 Visible = " <%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %> " > 尾页 </ asp:LinkButton >

另外,我要的要求是里面有个文本框,既显示当前页,又可以通过一个按钮跳转到文本框里输入的页码,却不知道如何传入这个页码,

本来应该产生如下形式的代码

  
    
1 CommandName = " Page " CommandArgument = " xxx "

 这个commandArgument我试过用<%# (((GridView)Container).FindControl("") as TextBox).Text %>的方式,结果显然得不到你手动输入的页码,最后还是简单地用了一个onClick来完成这个要求,即在onClick里面设置该button的 CommandName="Page" CommandArgument=页码文本框的值,搞定。

页面:

代码
   
     
1 < asp:TextBox ID = " txtcurrentPage " runat = " server " Text = " <%# ((GridView)Container.NamingContainer).PageIndex + 1 %> "
2 Width = " 20px " MaxLength = " 3 " CssClass = " txttopage " ></ asp:TextBox >
3   < asp:Button ID = " btngopage " runat = " server " Text = " go " OnClick = " getCustPage " OnClientClick = " return ToPage(); " />

 

后台:

代码
   
     
1 protected void getCustPage( object sender, EventArgs e)
2 {
3 Button btn = sender as Button;
4 btn.CommandName = " Page " ;
5 btn.CommandArgument = ((btn.NamingContainer as GridViewRow).FindControl( " txtcurrentPage " ) as TextBox).Text;
6 }

 

========其它===========

1,分页那一行叫做BottomPagerRow,需要到这一行找控件的话要注意。而且请注意,这一行是froeach gridview.rows找不到的,我曾经这样试过,结果无功而返。

2,所以,可以这样让分页行一直保持存在(有时候确实有这样的需求),响应前台GridView的onPreRender方法

代码
   
     
1 protected void renderview( object sender, EventArgs e)
2 {
3 GridViewRow gvr = (sender as GridView).BottomPagerRow;
4 if (gvr != null )
5 {
6 gvr.Visible = true ;
7 }
8 (gvr.FindControl( " ddlPageSize " ) as DropDownList).SelectedValue = gridList.PageSize.ToString();
9
10 PagedDataSource ps = new PagedDataSource();
11 ps.DataSource = ods.Select();
12 (gvr.FindControl( " lblTotalRecord " ) as Label).Text = ps.DataSourceCount.ToString();
13
14 this .hidcurpage.Value = (gridList.PageIndex + 1 ).ToString();
15 }

同时可以看到,因为gridview没有统计记录条数的方法,结果我还是用了pageddatasource,另外,数据重绑的时候貌似分页行的控件状态都刷新了,那么我只有在prerender的时候把它们全绑进去,如12,14行。

3,同1,表首行就应该是HeaderRow,总而言之,就是DataControlRowType的几个类型(DataRow,EmptyDataRow,Footer,Header,Pager,Separator)

你可能感兴趣的:(GridView)