GridView分頁的實現
復制代碼 代碼如下:要在GridView中加入
//實現分頁
AllowPaging="true"
//一頁數據
PageSize="
// 分頁時觸發的事件
OnPageIndexChanging="gvwDesignationName_PageIndexChanging"
在服務器事件裡
protectedvoid gvwDesignationName_PageIndexChanging(object sender
{
gvwDesignationName
bingDesignatioonName();
}
這裡我給出一個通用顯示分頁的模板(網上搜的
<PagerTemplate>
當前第:
//((GridView)Container
<asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container
頁/共:
//得到分頁頁面的總數
<asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container
頁
//如果該分頁是首分頁
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
Visible=
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev"
CommandName="Page" Visible=
//如果該分頁是尾頁
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
Visible=
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
Visible=
轉到第
<asp:TextBox ID="txtNewPageIndex" runat="server" Width="
//這裡將CommandArgument即使點擊該按鈕e
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="
CommandName="Page" Text="GO" />
</PagerTemplate>
對應該事件中代碼為
protected void gvwDesignationName_PageIndexChanging(object sender
{
// 得到該控件
GridView theGrid = sender as GridView;
int newPageIndex =
if (e
{
//點擊了Go按鈕
TextBox txtNewPageIndex = null;
//GridView較DataGrid提供了更多的API
GridViewRow pagerRow = theGrid
if (pagerRow != null)
{
//得到text控件
txtNewPageIndex = pagerRow
}
if ( txtNewPageIndex!= null)
{
//得到索引
newPageIndex = int
}
}
else
{
//點擊了其他的按鈕
newPageIndex = e
}
//防止新索引溢出
newPageIndex = newPageIndex <
newPageIndex = newPageIndex >= theGrid
//得到新的值
theGrid
//重新綁定
bingDesignatioonName();
}
From:http://tw.wingwit.com/Article/program/net/201311/14289.html