学习总结——gridview中实现分页

 实现如上图所示的分页,具体代码备忘:

前台:

ContractedBlock.gif ExpandedBlockStart.gif Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames
="REGIONCODE" AllowPaging="True" 
    onrowdatabound
="GridView1_RowDataBound" ondatabound="GridView1_DataBound" 
    onpageindexchanging
="GridView1_PageIndexChanging1" AllowSorting="True" 
    onsorting
="GridView1_Sorting" PageSize="15">

    
<Columns>
        
<asp:HyperLinkField  DataNavigateUrlFields="ID" 
            DataNavigateUrlFormatString
="~/notice/noticechakan.aspx?ID={0}" 
            DataTextField
="TITLE" HeaderText="通知标题" Target="_blank" Text="{0}" >
        
<HeaderStyle HorizontalAlign="Center" Width="80%" />
        
<ItemStyle HorizontalAlign="left" />
        
</asp:HyperLinkField>

        
<asp:BoundField DataField="SENDTIME" HeaderText="发送日期" 
            HeaderStyle-HorizontalAlign
="Center" ItemStyle-HorizontalAlign="Center" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False">
        
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>

<ItemStyle HorizontalAlign="Center"></ItemStyle>
        
</asp:BoundField>
        
        
<asp:BoundField DataField="REGIONCODE" HeaderText="用户" 
            HeaderStyle-HorizontalAlign
="Center" ItemStyle-HorizontalAlign="Center">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>

<ItemStyle HorizontalAlign="Center"></ItemStyle>
        
</asp:BoundField>

    
</Columns>


    
<PagerTemplate>
        
<table width="100%">
            
<tr>
                
<td width="70%">
                    
<asp:Label ID="lb_articlenum" runat="server" />
                    
<asp:LinkButton CommandName="Page" CommandArgument="First" ID="linkBtnFirst" runat="server">首页</asp:LinkButton>
                    
<asp:LinkButton CommandName="Page" CommandArgument="Prev" ID="linkBtnPrev" runat="server">上一页</asp:LinkButton>
                    
<asp:LinkButton CommandName="Page" CommandArgument="Next" ID="linkBtnNext" runat="server">下一页</asp:LinkButton>
                    
<asp:LinkButton CommandName="Page" CommandArgument="Last" ID="linkBtnLast" runat="server">末页</asp:LinkButton>
                    
<asp:Label ID="MessageLabel" ForeColor="Blue" Text="页码:" runat="server" />
                    
<asp:DropDownList ID="PageDropDownList" AutoPostBack="true" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged"
                        runat
="server" />
                
</td>
                
<td width="30%" align="right">
                    
<asp:Label ID="CurrentPageLabel" ForeColor="Blue" runat="server" />
                
</td>
            
</tr>
        
</table>
    
</PagerTemplate>


</asp:GridView>


<asp:Label ID="lb_sql" runat="server" Visible="False"></asp:Label>
<asp:Label ID="lb_count" runat="server" Visible="False"></asp:Label>

 

 后台代码:

ContractedBlock.gif ExpandedBlockStart.gif Code
 public void gridview_databind(string sql) 
        {
            BLLibrary.Bbase_notice noticedal 
= new BLLibrary.Bbase_notice();
            
            IList
<Model.base_noticeEntity> noticeEntitys = noticedal.Getbase_noticesbyCondition(sql+"order by sendtime desc");
           
            lb_count.Text 
= noticeEntitys.Count.ToString();
            GridView1.DataSource 
= noticeEntitys;
            GridView1.DataBind();
           
        }
//gridview的数据绑定


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex 
= e.NewPageIndex;
            
//重新绑定数据
            gridview_databind(lb_sql.Text);
        }

protected void GridView1_DataBound(object sender, EventArgs e)
        {
            GridViewRow pagerRow 
= GridView1.BottomPagerRow;
            
if (pagerRow != null)
            {
                LinkButton linkBtnFirst 
= (LinkButton)pagerRow.Cells[0].FindControl("linkBtnFirst");
                LinkButton linkBtnPrev 
= (LinkButton)pagerRow.Cells[0].FindControl("linkBtnPrev");
                LinkButton linkBtnNext 
= (LinkButton)pagerRow.Cells[0].FindControl("linkBtnNext");
                LinkButton linkBtnLast 
= (LinkButton)pagerRow.Cells[0].FindControl("linkBtnLast");

                
if (GridView1.PageIndex == 0)
                {
                    linkBtnFirst.Enabled 
= false;
                    linkBtnPrev.Enabled 
= false;
                }
                
else if (GridView1.PageIndex == GridView1.PageCount - 1)
                {
                    linkBtnLast.Enabled 
= false;
                    linkBtnNext.Enabled 
= false;
                }
                
else if (GridView1.PageCount <= 0)
                {
                    linkBtnFirst.Enabled 
= false;
                    linkBtnPrev.Enabled 
= false;
                    linkBtnNext.Enabled 
= false;
                    linkBtnLast.Enabled 
= false;
                }
                DropDownList pageList 
= (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
                Label pageLabel 
= (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
                Label articlenum 
= (Label)pagerRow.Cells[0].FindControl("lb_articlenum");
                articlenum.Text 
= "共有" + lb_count.Text + "条数据,本页" + GridView1.Rows.Count.ToString() + "条数据  ";
                
if (pageList != null)
                {
                    
for (int i = 0; i < GridView1.PageCount; i++)
                    {
                        
int pageNumber = i + 1;
                        ListItem item 
= new ListItem("" + pageNumber.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();
                }

            }
        }


protected void PageDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow pagerRow 
= GridView1.BottomPagerRow;
            DropDownList pageList 
= (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
            GridView1.PageIndex 
= pageList.SelectedIndex;
            gridview_databind(lb_sql.Text);
        }

 

这个是在做一个项目时用到的,自己留下来备忘。

转载于:https://www.cnblogs.com/lostcosta/archive/2009/10/28/1591472.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值