GridView控件,前台 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="20" OnPageIndexChanging="GridView1_PageIndexChanging" > <Columns> ...... </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> 后台代码: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; DataSet ds = this.getData(); //获得数据源DataSet theGrid.DataSource = ds; //重新绑定 theGrid.DataBind(); // 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
/// 产生翻页导航HTML
/// </summary>
/// <param name="totalEntries">条目总数</param>
/// <param name="pageSize">页条目大小</param>
/// <param name="currentPage">当前页码</param>
/// <param name="navigationUrl">导航URL, 如 view.aspx?cat=aspx</param>
/// <param name="classNameForInvalid">为无效导航元素添加 CSS 类名</param>
/// <returns>HTML代码</returns>
public static string GeneralPageNavigation(int totalEntries, int pageSize, int currentPage,
string firstPageFormat, string lastPageFormat,
string nextPageFormat, string previousPageFormat,
string totalPagesFormat, string nowAtFormat, string classNameForInvalid)
{
int totalPages;
StringBuilder sbAll = new StringBuilder();
/*
* 两种情况
* 条目记录 <= 每页数量
*/
if (totalEntries < pageSize || totalEntries == pageSize)
{
sbAll.Append("<span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">首页</span> | <span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">上一页</span> | <span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">下一页</span> | <span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">尾页</span> - 共 1 页 | 正在第 1 页</span>");
return sbAll.ToString();
}
else
{
if (totalEntries % pageSize != 0)
totalPages = totalEntries / pageSize + 1;
else
totalPages = totalEntries / pageSize;
sbAll.Append(string.Format(firstPageFormat, 1));
sbAll.Append(" | ");
if (currentPage == 1)
{
sbAll.Append("<span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">上一页</span> | ");
}
else
{
sbAll.Append(string.Format(previousPageFormat, currentPage - 1));
sbAll.Append(" | ");
} // end of if
if (currentPage < totalPages)
{
sbAll.Append(string.Format(nextPageFormat, currentPage + 1));
sbAll.Append(" | ");
}
else
{
sbAll.Append("<span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">下一页</span> | ");
} // end of if
sbAll.Append(string.Format(lastPageFormat, totalPages));
sbAll.Append(string.Format(totalPagesFormat, totalPages));
sbAll.Append(string.Format(nowAtFormat, currentPage));
return sbAll.ToString();
} // end of if
} public static string GeneralPageNavigation(int totalEntries, int pageSize, int currentPage,
string navigationUrl, string classNameForInvalid,
string firstPage, string lastPage, string previousPage,
string nextPage, string totalPagesFormat, string nowAt)
{
int totalPages;
StringBuilder sbAll = new StringBuilder();
/*
* 两种情况
* 条目记录 <= 每页数量
*/
if (totalEntries < pageSize || totalEntries == pageSize)
{
sbAll.Append("<span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">");
sbAll.Append(firstPage);
sbAll.Append("</span> | <span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">");
sbAll.Append(nextPage);
sbAll.Append("</span> | <span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">");
sbAll.Append(previousPage);
sbAll.Append("</span> | <span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">");
sbAll.Append(lastPage);
sbAll.Append("</span> - ");
sbAll.Append(string.Format(totalPagesFormat, 1));
sbAll.Append(string.Format(nowAt, 1));
sbAll.Append("</span>");
return sbAll.ToString();
}
else
{
totalPages = totalEntries / pageSize + 1;
sbAll.Append("<a href=\"");
sbAll.Append(navigationUrl);
sbAll.Append("&page=1\">");
sbAll.Append(firstPage);
sbAll.Append("</a> | ");
if (currentPage == 1)
{
sbAll.Append("<span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">");
sbAll.Append(previousPage);
sbAll.Append("</span> | ");
}
else
{
sbAll.Append("<a href=\"");
sbAll.Append(navigationUrl);
sbAll.Append("&page=");
sbAll.Append(currentPage - 1);
sbAll.Append("\">");
sbAll.Append(previousPage);
sbAll.Append("</a> | ");
} // end of if if (currentPage < totalPages)
{
sbAll.Append("<a href=\"");
sbAll.Append(navigationUrl);
sbAll.Append("&page=");
sbAll.Append(currentPage + 1);
sbAll.Append("\">");
sbAll.Append(nextPage);
sbAll.Append("</a> | ");
}
else
{
sbAll.Append("<span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">");
sbAll.Append(nextPage);
sbAll.Append("</span> | ");
} // end of if
sbAll.Append("<a href=\"");
sbAll.Append(navigationUrl);
sbAll.Append("&page=");
sbAll.Append(totalPages);
sbAll.Append("\">");
sbAll.Append(lastPage);
sbAll.Append("</a> - ");
sbAll.Append(string.Format(totalPagesFormat, totalPages));
sbAll.Append(" | ");
sbAll.Append(string.Format(nowAt, currentPage)); return sbAll.ToString();
} // end of if
}
/// <summary>
/// 产生翻页导航HTML
/// </summary>
/// <param name="totalEntries">条目总数</param>
/// <param name="pageSize">页条目大小</param>
/// <param name="currentPage">当前页码</param>
/// <param name="navigationUrl">导航URL, 如 view.aspx?cat=aspx</param>
/// <param name="classNameForInvalid">为无效导航元素添加 CSS 类名</param>
/// <returns>HTML代码</returns>
public static string GeneralPageNavigation(int totalEntries, int pageSize, int currentPage,
string firstPageFormat, string lastPageFormat,
string nextPageFormat, string previousPageFormat,
string totalPagesFormat, string nowAtFormat, string classNameForInvalid)
{
int totalPages;
StringBuilder sbAll = new StringBuilder();
/*
* 两种情况
* 条目记录 <= 每页数量
*/
if (totalEntries < pageSize || totalEntries == pageSize)
{
sbAll.Append("<span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">首页</span> | <span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">上一页</span> | <span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">下一页</span> | <span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">尾页</span> - 共 1 页 | 正在第 1 页</span>");
return sbAll.ToString();
}
else
{
if (totalEntries % pageSize != 0)
totalPages = totalEntries / pageSize + 1;
else
totalPages = totalEntries / pageSize;
sbAll.Append(string.Format(firstPageFormat, 1));
sbAll.Append(" | ");
if (currentPage == 1)
{
sbAll.Append("<span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">上一页</span> | ");
}
else
{
sbAll.Append(string.Format(previousPageFormat, currentPage - 1));
sbAll.Append(" | ");
} // end of if
if (currentPage < totalPages)
{
sbAll.Append(string.Format(nextPageFormat, currentPage + 1));
sbAll.Append(" | ");
}
else
{
sbAll.Append("<span class=\"");
sbAll.Append(classNameForInvalid);
sbAll.Append("\">下一页</span> | ");
} // end of if
sbAll.Append(string.Format(lastPageFormat, totalPages));
sbAll.Append(string.Format(totalPagesFormat, totalPages));
sbAll.Append(string.Format(nowAtFormat, currentPage));
return sbAll.ToString();
} // end of if
这么好的分页控件都不用,准备用什么啊
-----------
呵呵,那方法我写了三个重载,也不知道哪个好,效率不同,灵活程度不同.
使用很简单的,你只要传递几个参数而已.
直接跳转到第XX页是另外一段HTML+JS的.
<input type="text" id="txtTargetPage" class="CText"
onkeypress="if (event.keyCode==13) {changePage('category.aspx?cat=<%=curCat %>',this.value);return false;}"
onmouseover='this.focus()' /><input type="button" value="GO" class="CButton"
onclick="changePage('category.aspx?cat=<%=curCat %>',txtTargetPage.value)" />[code]
[code=JScript]
function changePage(sUrlPrefix, sTargetPage){
if(!isNaN(sTargetPage)) location.href=sUrlPrefix + "&page=" + sTargetPage;
}
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="20" OnPageIndexChanging="GridView1_PageIndexChanging" >
<Columns>
......
</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>
后台代码: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; DataSet ds = this.getData(); //获得数据源DataSet
theGrid.DataSource = ds; //重新绑定
theGrid.DataBind(); // 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
}