GridView分页

首先添加一个GridView的模板列

前台:

// 添加GridView模板列
< PagerTemplate >
                            
< asp:Label ID = " CurrentPageLabel "  ForeColor = " Blue "  runat = " server "  Text = " 当前页: "   />& nbsp;
                            
< asp:LinkButton id = " FirstPage "  runat = " server "  CommandArgument = " FIRST "  CommandName = " Page " > 首页 </ asp:LinkButton >
                            
< asp:linkbutton  id = " PreviousPage "  runat = " server "  CommandArgument = " Prev "  CommandName = " Page " > 上一页 </ asp:linkbutton >
                            
< asp:linkbutton  id = " NextPage "   runat = " server "  CommandArgument = " NEXT "  CommandName = " Page " > 下一页 </ asp:linkbutton >
                            
< asp:linkbutton  id = " LastPage "   runat = " server "  CommandArgument = " LAST "  CommandName = " Page " > 尾页 </ asp:linkbutton >
                            
< asp:label id = " Lab3 "   runat = " server "  ForeColor = " Blue "  Height = " 7px "  Font - Size = " 12px " ></ asp:label >
                            
< asp:Label ID = " MessageLabel "  ForeColor = " Blue "  Text = " 页码: "  runat = " server "   />
                            
< asp:DropDownList ID = " PageDropDownList "  AutoPostBack = " true "  runat = " server "  OnSelectedIndexChanged = " PageDropDownList_SelectedIndexChanged "   />& nbsp;
                        
</ PagerTemplate >

 后台:

protected   void  GridView1_DataBound( object  sender, EventArgs e)
    
{
        
try
        
{
            GridViewRow pagerRow 
= GridView1.BottomPagerRow;

            LinkButton FirstPage 
= (LinkButton)pagerRow.Cells[0].FindControl("FirstPage");
            LinkButton PreviousPage 
= (LinkButton)pagerRow.Cells[0].FindControl("PreviousPage");
            LinkButton NextPage 
= (LinkButton)pagerRow.Cells[0].FindControl("NextPage");
            LinkButton LastPage 
= (LinkButton)pagerRow.Cells[0].FindControl("LastPage");

            
if (GridView1.PageIndex == 0)
            
{
                FirstPage.Enabled 
= false;
                PreviousPage.Enabled 
= false;
            }

            
else if (GridView1.PageIndex == GridView1.PageCount - 1)
            
{
                LastPage.Enabled 
= false;
                NextPage.Enabled 
= false;
            }

            
else if (GridView1.PageCount <= 0)
            
{
                FirstPage.Enabled 
= false;
                PreviousPage.Enabled 
= false;
                NextPage.Enabled 
= false;
                LastPage.Enabled 
= false;
            }

            DropDownList pageList 
= (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
            Label pageLabel 
= (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");

            
if (pageList != null)
            
{
                
for (int i = 0; i < GridView1.PageCount; i++)
                
{
                    
int pageNumber = i + 1;
                    ListItem item 
= new ListItem(pageNumber.ToString() + "/" + GridView1.PageCount.ToString(), pageNumber.ToString());
                    
if (i == GridView1.PageIndex)
                    
{
                        item.Selected 
= true;
                    }

                    pageList.Items.Add(item);

                }


            }

            
if (pageLabel != null)
            
{
                
int currentPage = GridView1.PageIndex + 1;
                pageLabel.Text 
= "当前页:" + currentPage.ToString() +
                  
" / " + GridView1.PageCount.ToString();
            }


     
catch { }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值