.aspx页面:
PageIndexChanging处理程序:
<
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 >
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
}
{
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
}