日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

自定义GridView分页模板

發布時間:2023/11/27 生活经验 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义GridView分页模板 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

GridView較之DataGrid提供了更加強大、更加完善的功能,而且具備了豐富的可擴展功能,可以使用GridView提供的pagertemplate自定義分頁模板:

事實上,GridView默認的幾中分頁樣式,都是將相關按鈕的CommandName設為Page,而CommandArgument設為相關參數,可接受的參數包括,first,last,prev,next,<PageIndex>(具體數字),然后按事件回溯,觸發頂層的RowCommand,因此我們頁可以使用這些默認的可識別的參數自定義自己的分頁模板,asp.net會自動設置當前的NewPageIndex,而不需要任何的冗余代碼。

.aspx頁面:


<asp:gridview id="GridView1" runat="server" allowpaging="True" pagesize="10"
??????????? autogeneratecolumns="False" datasourceid="SqlDataSource1"
??????????? onpageindexchanging="GridView1_PageIndexChanging">
??????????? <columns>
??????????????? <asp:boundfield datafield="CompanyName" headertext="CompanyName" sortexpression="CompanyName" />
??????????????? <asp:boundfield datafield="ContactTitle" headertext="ContactTitle" sortexpression="ContactTitle" />
??????????????? <asp:boundfield datafield="Phone" headertext="Phone" sortexpression="Phone" />
??????????????? <asp:boundfield datafield="Fax" headertext="Fax" sortexpression="Fax" />
??????????????? <asp:boundfield datafield="ContactName" headertext="ContactName" sortexpression="ContactName" />
??????????? </columns>
?????????????????????? <pagertemplate>
??????????????????????? <table width="100%">
????????????????????????? <tr>
??????????????????????????? <td style="text-align:right">
??????????????????????????? 第<asp:Label id="lblPageIndex" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1? %>' />頁
??????????????????????????????? 共/<asp:Label id="lblPageCount" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageCount? %>' />頁
??????????????????????????????? <asp:linkbutton id="btnFirst" runat="server" causesvalidation="False" commandargument="First" commandname="Page" text="首頁" />
????????????????????????????? <asp:linkbutton id="btnPrev" runat="server" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一頁" />
???????????????????????????? <asp:linkbutton id="btnNext" runat="server" causesvalidation="False" commandargument="Next" commandname="Page" text="下一頁" />?????????????????????????
???????????????????????????? <asp:linkbutton id="btnLast" runat="server" causesvalidation="False" commandargument="Last" commandname="Page" text="尾頁" />???????????????????????????????????????????
???????????????????????????? <asp:textbox id="txtNewPageIndex" runat="server" width="20px" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1? %>' />
???????????????????????????? <asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="GO" /><!-- here set the CommandArgument of the Go Button to '-1' as the flag -->
???????????????????????????? </td>
????????????????????????? </tr>
??????????????????????? </table>
??????????????????? </pagertemplate>
??????? </asp:gridview>
???
???
??????? <asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
??????????? providername="System.Data.SqlClient" selectcommand="SELECT [CompanyName], [ContactTitle], [Phone], [Fax], [ContactName] FROM [Customers]">
??????? </asp:sqldatasource>
PageIndexChanging處理程序:
??? protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
??? {
??????? GridView theGrid = sender as GridView;? // refer to the GridView
??????? int newPageIndex = 0;

??????? if (-2 == e.NewPageIndex) { // when click the "GO" Button
??????????? TextBox txtNewPageIndex = null;
??????????? //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView較DataGrid提供了更多的API,獲取分頁塊可以使用BottomPagerRow 或者TopPagerRow,當然還增加了HeaderRow和FooterRow
//updated at 2006年6月21日3:15:33

??????????? if (null != pagerRow) {
??????????????? txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;?? // refer to the TextBox with the NewPageIndex value
??????????? }

??????????? if (null != txtNewPageIndex) {
??????????????? newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
??????????? }
??????? }
??????? else {? // when click the first, last, previous and next Button
??????????? newPageIndex = e.NewPageIndex;
??????? }

??????? // check to prevent form the NewPageIndex out of the range
??????? newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
??????? newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

??????? // specify the NewPageIndex
??????? theGrid.PageIndex = newPageIndex;

??????? // rebind the control
??????? // in this case of retrieving the data using the xxxDataSoucr control,
??????? // just do nothing, because the asp.net engine binds the data automatically
??? }
注意到,上面的示例中,由于增加了一個跳轉按鈕GO,但是asp。net不支持相關的CommandArgument值,雖然可以將Go Button的Commandname設為Page,還需要手動的在PageIndexChanging增加部分處理邏輯。

?

總結

以上是生活随笔為你收集整理的自定义GridView分页模板的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。