实现GridView的自定义分页



GridView自带几种分页,如果不能满足要求的话,可以看看下面的代码

   < asp:GridView ID = " gvNewsList "  AllowPaging = " true "   OnPageIndexChanging = " gvNewsList_PageIndexChanging "  PageSize = " 1 "  style = " BORDER-RIGHT: medium none; BORDER-LEFT: medium none; WORD-BREAK: break-all "  runat = " server "  AutoGenerateColumns = " False "  Width = " 96% "  HorizontalAlign = " Center "  CellPadding = " 4 "  CssClass = " border "  ForeColor = " #333333 "  GridLines = " Both "  BorderColor = " black " >
            
< HeaderStyle BackColor = " #507CD1 "  BorderWidth = " 0px "  CssClass = " table04bg "  Font - Bold = " True "  ForeColor = " black "   />             
            
< Columns >
            
                
< asp:CheckBoxField HeaderText = " 选择 "   />
                
< asp:BoundField DataField = " Newsid "  HeaderText = " 编号 "   />
                
< asp:HyperLinkField DataTextField = " title "  HeaderText = " 标题 "   />
                
< asp:BoundField DataField = " ClassName "  HeaderText = " 类别 "   />
                
< asp:BoundField DataField = " update_date "  HeaderText = " 更新时间 "   />
            
</ Columns >
           
< RowStyle BackColor = " white "  HorizontalAlign = " center "   BorderColor = " black "  Font - Names = " 宋体 " />
            
< pagertemplate >
                        
< table width = " 100% " >
                          
< tr >
                            
< td style = " text-align:right " >
                            第
< asp:Label id = " lblPageIndex "  runat = " server "  text = ' <%# ((GridView)Container.Parent.Parent).PageIndex + 1  %> '   />
                                共
/< asp:Label id = " lblPageCount "  runat = " server "  text = ' <%# ((GridView)Container.Parent.Parent).PageCount  %> '   /> 页 
                                
< asp:linkbutton id = " btnFirst "  runat = " server "  causesvalidation = " False "  commandargument = " First "  commandname = " Page "  text = " 首页 "   />
                              
< asp:linkbutton id = " btnPrev "  runat = " server "  causesvalidation = " False "  commandargument = " Prev "  commandname = " Page "  text = " 上一页 "   />
                             
< asp:linkbutton id = " btnNext "  runat = " server "  causesvalidation = " False "  commandargument = " Next "  commandname = " Page "  text = " 下一页 "   />                           
                             
< asp:linkbutton id = " btnLast "  runat = " server "  causesvalidation = " False "  commandargument = " Last "  commandname = " Page "  text = " 尾页 "   />                                             
                             
< asp:textbox id = " txtNewPageIndex "  runat = " server "  width = " 20px "  text = ' <%# ((GridView)Container.Parent.Parent).PageIndex + 1  %> '   />
                             
< asp:linkbutton id = " btnGo "  runat = " server "  causesvalidation = " False "  commandargument = " -1 "  commandname = " Page "  text = " GO "   /><!--  here  set  the CommandArgument of the Go Button to  ' -1 '   as  the flag  -->
                             
</ td >
                          
</ tr >
                        
</ table >
                    
</ pagertemplate >
                    
< PagerStyle BackColor = " #507CD1 "  BorderWidth = " 0px "  CssClass = " table04bg "  Font - Bold = " True "  ForeColor = " black "   />  

        
</ asp:GridView >

上面代码中<pagertemplate>与</pagertemplate>之间为自定义分页,下面看一下触发的事件代码

 1 protected   void  gvNewsList_PageIndexChanging( object  sender, GridViewPageEventArgs e)
 2          {
 3            GridView theGrid = sender as GridView;  // refer to the GridView
 4            int newPageIndex = 0;
 5
 6            if (-2 == e.NewPageIndex)
 7            // when click the "GO" Button
 8                TextBox txtNewPageIndex = null;
 9                //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
10                GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
11                //updated at 2006年6月21日3:15:33
12
13                if (null != pagerRow)
14                {
15                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex"as TextBox;   // refer to the TextBox with the NewPageIndex value
16                }

17
18                if (null != txtNewPageIndex)
19                {
20                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1// get the NewPageIndex
21                }

22            }

23            else
24            {  // when click the first, last, previous and next Button
25                newPageIndex = e.NewPageIndex;
26            }

27
28            // check to prevent form the NewPageIndex out of the range
29            newPageIndex = newPageIndex <= 0 ? 0 : newPageIndex;
30            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount-1 : newPageIndex;
31
32            // specify the NewPageIndex
33            theGrid.PageIndex = newPageIndex;
34            BindNewsList();
35            // rebind the control
36            // in this case of retrieving the data using the xxxDataSoucr control,
37            // just do nothing, because the asp.net engine binds the data automatically
38        }

注意代码中最后的内容,一定要从新绑定 GridView这样分页才能够生效

转载于:https://www.cnblogs.com/xiaotao823/archive/2008/01/16/1041581.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值