溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么實(shí)現(xiàn)GridView分頁

發(fā)布時(shí)間:2021-10-14 14:10:13 來源:億速云 閱讀:118 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)怎么實(shí)現(xiàn)GridView分頁的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

要在GridView中加入
//實(shí)現(xiàn)分頁
AllowPaging="true"
//一頁數(shù)據(jù)10行
PageSize="10"
// 分頁時(shí)觸發(fā)的事件
OnPageIndexChanging="gvwDesignationName_PageIndexChanging"
服務(wù)器事件里

復(fù)制代碼 代碼如下:


protectedvoid gvwDesignationName_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvwDesignationName.PageIndex=e.newIndex;
bingDesignatioonName();
}


這里我給出一個(gè)通用顯示分頁的模板(網(wǎng)上搜的,自己給出注釋)

復(fù)制代碼 代碼如下:


<PagerTemplate>
當(dāng)前第:
//((GridView)Container.NamingContainer)就是為了得到當(dāng)前的控件
<asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
頁/共:
//得到分頁頁面的總數(shù)
<asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>

//如果該分頁是首分頁,那么該連接就不會(huì)顯示了.同時(shí)對(duì)應(yīng)了自帶識(shí)別的命令參數(shù)CommandArgument
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首頁</asp:LinkButton>
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev"
CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一頁</asp:LinkButton>
//如果該分頁是尾頁,那么該連接就不會(huì)顯示了
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一頁</asp:LinkButton>
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾頁</asp:LinkButton>
轉(zhuǎn)到第
<asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />頁
//這里將CommandArgument即使點(diǎn)擊該按鈕e.newIndex 值為3
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-2"
CommandName="Page" Text="GO" />
</PagerTemplate>


對(duì)應(yīng)該事件中代碼為

復(fù)制代碼 代碼如下:


protected void gvwDesignationName_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// 得到該控件
GridView theGrid = sender as GridView;
int newPageIndex = 0;
if (e.NewPageIndex==-3)
{
//點(diǎn)擊了Go按鈕
TextBox txtNewPageIndex = null;
//GridView較DataGrid提供了更多的API,獲取分頁塊可以使用BottomPagerRow 或者TopPagerRow,當(dāng)然還增加了HeaderRow和FooterRow
GridViewRow pagerRow = theGrid.BottomPagerRow;
if (pagerRow != null)
{
//得到text控件
txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
}
if ( txtNewPageIndex!= null)
{
//得到索引
newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
}
}
else
{
//點(diǎn)擊了其他的按鈕
newPageIndex = e.NewPageIndex;
}
//防止新索引溢出
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
//得到新的值
theGrid.PageIndex = newPageIndex;
//重新綁定
bingDesignatioonName();
}

感謝各位的閱讀!關(guān)于“怎么實(shí)現(xiàn)GridView分頁”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI