ASPNETPager常用属性
建议去封装好,然后调用这样比较容易
alwaysshow="True" PageSize="5" custominfosectionwidth="20%" custominfotextalign="Right" firstpagetext="第一页" horizontalalign="Left" lastpagetext="末一页" navigationbuttontype="Image" nextpagetext="后一页" pageindexboxtype="TextBox" pagingbuttonspacing="8px" prevpagetext="前一页" showcustominfosection="Right" showpageindexbox="Always" textafterpageindexbox="页" UrlPaging="true" textbeforepageindexbox="跳到第" width="97%" onpagechanged="AspNetPager1_PageChanged">
alwaysshow:总是显示分页控件;
PageSize:指定每页显示的记录数;
custominfosectionwidth:用户自定义信息区的宽度;
custominfotextalign:用户自定义信息区的对齐方式;
firstpagetext:第一页按钮上显示的文本;
horizontalalign:内容水平对齐方式;
lastpagetext:最后页按钮上显示的文本;
navigationbuttontype:第一页、下一页、最后一页按钮的类型;
nextpagetext:下一页按钮上显示的文本;
pageindexboxtype:指示页索引框的显示类型:有TextBOX和dropdownlist两种;
pagingbuttonspacing:导航页按钮的间距;
prevpagetext:上一页按钮的显示的文本;
showcustominfosection:显示当前页和总页信息,默认为不显示,值为LEFT时将显示在页索引前,为right时显示在页索引后;
showpageindexbox:指定页索引文本框或下拉框的显示方式;
textafterpageindexbox:指定页索引文本框或下拉框后的文本;
UrlPaging:是够使用URL传递分页的方式来分页;
textbeforepageindexbox:指定页索引文本框或下拉框前面显示的文本;
width:该控件的宽度;
CustomInfoHTML:指定要显示在用户自定义信息区的用户自定义HTML信息文本
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
private void Bind()
{
int pageindex = 1;
int pagenum = AspNetPager1.PageSize;
if (Request["page"] != null)
{
pageindex =Convert.ToInt32(Request["page"]);
}
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString ());
SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
SqlDataAdapter sda = new SqlDataAdapter("select top (@pagenum) * from pagetest where id not in (select top (@pagenum*(@pageindex-1)) id from pagetest)", con);
sda.SelectCommand.Parameters.AddWithValue("pagenum",pagenum);
sda.SelectCommand.Parameters.AddWithValue("pageindex",pageindex);
sda.Fill(ds);
SqlCommand cmd = new SqlCommand("select count(*) from pagetest", con1);
con1.Open();
AspNetPager1.RecordCount =Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
AspNetPager1.CustomInfoHTML = "共" + AspNetPager1.RecordCount + "条记录 " + AspNetPager1.CurrentPageIndex + "/" + AspNetPager1.PageCount;
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
Bind();
}
}
深入分析CurrentPageIndex、RecordCount、 PageCount、 PageSize:
pagecount:
public int PageCount
{
get
{
if (this.RecordCount == 0)
{
return 1;
}
return (int) Math.Ceiling((double) (((double) this.RecordCount) / ((double) this.PageSize)));
}
}
recordcount:
public int RecordCount
{
get
{
if (this.cloneFrom != null)
{
return this.cloneFrom.RecordCount;
}
object obj2 = this.ViewState["Recordcount"];
if (obj2 != null)
{
return (int) obj2;
}
return 0;
}
set
{
this.ViewState["Recordcount"] = value;
}
}
CurrentPageIndex:
public int CurrentPageIndex
{
get
{
if (this.cloneFrom != null)
{
return this.cloneFrom.CurrentPageIndex;
}
object obj2 = this.ViewState["CurrentPageIndex"];
int num = (obj2 == null) ? 1 : ((int) obj2);
if ((num > this.PageCount) && (this.PageCount > 0))
{
return this.PageCount;
}
if (num < 1)
{
return 1;
}
return num;
}
set
{
int pageCount = value;
if (pageCount < 1)
{
pageCount = 1;
}
else if (pageCount > this.PageCount)
{
pageCount = this.PageCount;
}
this.ViewState["CurrentPageIndex"] = pageCount;
}
}
PageSize:
public int PageSize
{
get
{
int num;
if ((!string.IsNullOrEmpty(this.UrlPageSizeName) && !base.DesignMode) && (int.TryParse(this.Page.Request.QueryString[this.UrlPageSizeName], out num) && (num > 0)))
{
return num;
}
if (this.cloneFrom != null)
{
return this.cloneFrom.PageSize;
}
object obj2 = this.ViewState["PageSize"];
if (obj2 != null)
{
return (int) obj2;
}
return 10;
}
set
{
this.ViewState["PageSize"] = value;
}
}
AlwaysShow
获取或设置一个值,该值指定是否总是显示AspNetPager分页按件,即使要分页的数据只有一页。
CurrentPageButtonPosition
当前页数字按钮在所有数字分页按钮中的位置,可选值为:Beginning(最前)、End(最后)、Center(居中)和Fixed(默认固定)
CurrentPageIndex
获取或设置当前显示页的索引。
FirstPageText
获取或设置为第一页按钮显示的文本。
LastPageText
获取或设置为最后一页按钮显示的文本。
NextPageText
获取或设置为下一页按钮显示的文本。
PrevPageText
获取或设置为上一页按钮显示的文本。
PageSize
获取或设置每页显示的项数。(该值获取或设置数据呈现控件每次要显示数据表中的的数据的项数,AspNetPager根据该值和 RecordCount 来计算显示所有数据需要的总页数,即 PageCount的值。 )
PageIndexBoxType:
或者或设置页索引框的显示类型,可以是允许用户手工输入的文本框和只能选择的下拉框。
MoreButtonType
获取或设置“更多页”(...)按钮的类型,该值仅当PagingButtonType设为Image时才有效。
UrlPageIndexName
获取或设置当启用Url分页方式时,在url中表示要传递的页索引的参数的名称。
UrlPageSizeName
获取或设置Url中指定每页显示记录数的参数的名称,或该值不为空或Url
中该值对应的参数的值大于0,则PageSize属性将使用该参数的值做为每页显示的记录数。
UrlPaging
获取或设置是否启用url来传递分页信息。
TextAfterPageIndexBox
获取或设置页索引页索引输入文本框或下拉框后的文本字符串值。
TextBeforePageIndexBox
获取或设置页索引页索引输入文本框或下拉框前的文本字符串值。
NavigationButtonType
获取或设置第一页、上一页、下一页和最后一页按钮的类型,该值仅当PagingButtonType设为Image时才有效。
NavigationToolTipTextFormatString
获取或设置导航按钮工具提示文本的格式。
NextPageText
获取或设置为下一页按钮显示的文本。
NumericButtonCount
获取或设置在 AspNetPager 控件的页导航元素中同时显示的数值按钮的数目。
NumericButtonTextFormatString
获取或设置页索引数值导航按钮上文本的显示格式。
NumericButtonType
获取或设置页导航数值按钮的类型,该值仅当PagingButtonType设为Image时才有效。
PageCount
获取所有要分页的记录需要的总页数。
PageIndexBoxClass
获取或设置应用于页索引输入文本框或下拉框的CSS类名。
PageIndexBoxStyle
获取或设置页索引输入文本框或下拉框的CSS样式文本。
PageIndexBoxType
PageIndexOutOfRangeErrorMessage
获取或设置当用户输入的页索引超出范围(大于最大页索引或小于最小页索引)时在客户端显示的错误信息。
PageSize 获取或设置每页显示的项数。
PagesRemain 获取当前页之后未显示的页的总数。 PagingButtonLayoutType
指定分页导航按钮(数字和上页、下页、首页、尾页)布局方式,可以将这些元素包含在
PagingButtonSpacing
获取或设置分页导航按钮之间的间距。
PagingButtonType
获取或设置分页导航按钮的类型,即使用文字还是图片。
ShowFirstLast
获取或设置一个值,该值指示是否在页导航元素中显示第一页和最后一页按钮。
ShowNavigationToolTip
获取或设置一个值,该值批示当鼠标指针悬停在导航按钮上时是否显示工具提示。
ShowPageIndex
获取或设置一个值,该值指示是否在页导航元素中显示页索引数值按钮。
ShowPageIndexBox ShowPrevNext
获取或设置一个值,该值指示是否在页导航元素中显示上一页和下一页按钮