gridview中實現分頁

iDotNetSpace發表於2009-10-28

前臺:

gridview中實現分頁
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt<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>

 

 後臺程式碼:

gridview中實現分頁
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt 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);
        }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-617607/,如需轉載,請註明出處,否則將追究法律責任。

相關文章