原文地址为:
分页控件AspNetPager的用法
<
webdiyer:AspNetPager
ID
="AspNetPager1"
runat
="server"
AlwaysShow
="True"
//总是显示分页控件,即使要分页的数据只有一页
OnPageChanged ="AspNetPager1_PageChanged" //分页发生改变时触发事件
UrlPaging
="true"
//通过URL传递分页信息的方式来分页。如果设为true,禁用ViewState也能达到效果。如果设置为false,禁用了viewstate则无法实现分页.
NumericButtonTextFormatString="[{0}]" //页索引数字显示的格式
ShowCustomInfoSection ="Left" > //显示当前页和总页数信息,默认值不显示,为left则将显示在页索引前,为right则为页索引后 </ webdiyer:AspNetPager >3.Page_Load事件:
protected
void
Page_Load(
object
sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (!Page.IsPostBack)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BindGridView();
}
}
BindGridView方法如下:
public
void
BindGridView()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string sql = "select * from gw_newsletter_group";//自定义的SQL语句
int recordcount;
DataSet ds = GetPage(sql, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize,out recordcount);
this.AspNetPager1.RecordCount = recordcount;
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
AspNetPager1.CustomInfoHTML = "记录总数:<b>" + AspNetPager1.RecordCount.ToString() + "</b>";
AspNetPager1.CustomInfoHTML += " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>";
AspNetPager1.CustomInfoHTML += " 当前页:<font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
}
其中的GetPage方法代码如下:(
GetConnection()是返回一个SqlConnection对象)
/**/
/// <summary>
/// Get Data By CurrentPage
/// </summary>
/// <param name="sql">SQL</param>
/// <param name="currentPage">current Page</param>
/// <param name="pagesize">Page Size</param>
/// <param name="recordcount">Total Record</param>
/// <returns></returns>
public
DataSet GetPage(
string
sql,
int
currentPage,
int
pagesize,
out
int
recordcount)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
SqlDataAdapter ada = new SqlDataAdapter(sql, GetConnection());
DataSet ds = new DataSet();
int startRow = (currentPage - 1) * pagesize;
ada.Fill(ds, startRow, pagesize, "table");
recordcount = GetPageRecord(sql);
return ds;
}
GetPageRecord方法:(
返回总的记录数)
public
int
GetPageRecord(
string
sql)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
sql = System.Text.RegularExpressions.Regex.Replace(sql, "ORDER BY.*", "");
sql = "select count(*) from (" + sql + ") as temp";
SqlCommand cmd = new SqlCommand(sql, GetConnection());
cmd.Connection.Open();
int recordcount = (int)cmd.ExecuteScalar();
return recordcount;
}
这里注意要去掉”Order By“的字符串(
聚合函数不能和"ORDER BY"连用)
4.另外:还有个事件:
protected
void
AspNetPager1_PageChanged(
object
sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
BindGridView();
![](/Images/OutliningIndicators/InBlock.gif)
}
到目前为止就可以运行看到效果:
地址栏显示效果:
注意这里是把属性: urlpaging=true,而且如果禁止了ViewState也能实现。
如果把 urlpaging=false 的话
如果禁止了ViewState就无法实现分页功能了。
上面的代码中有这样一句话:
AspNetPager1.CustomInfoHTML
+=
"
总页数:<b>
"
+
AspNetPager1.PageCount.ToString()
+
"
</b>
"
;
这里就有一个问题是aspNetPage1.PageCount怎样得出来的呢?我们并没有为它赋值。(这里而且这个属性是只读的)
实际上它的值是本身的控件就实现了:我分析觉得代码可能如下:
public
int
GetPagerCount(
string
sql,
int
pagesize)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int recordcount = GetPageRecord(sql);//方法见上
int pagecount=recordcount/pagesize;
return (recordcount % pagesize == 0 ? pagecount : pagecount + 1);
}
这样就可以得出到底分页的页数.
最后补充说明:由于我只是初步研究这个控件,不过上面的代码都是经过测试通过的。
而且我还发现这个控件还有很多属性可以改变它的样式外观的。而且还比较灵活的。希望能给朋友们带来一些帮助。
另外如果有什么地方不正确还希望高手能及时的提出来!
转载请注明本文地址: 分页控件AspNetPager的用法
分页是Web应用程序中最常用到的功能之一,在ASP.NET中,虽然自带了一个可以分页的DataGrid(asp.net 1.1)和GridView(asp.net 2.0)控件,但其分页功能并不尽如人意,如可定制性差、无法通过Url实现分页功能等,而且有时候我们需要对DataList和Repeater甚至自定义数据绑定控件进行分页,手工编写分页代码不但技术难度大、任务繁琐而且代码重用率极低,因此分页已成为许多ASP.NET程序员最头疼的问题之一。
今天闲来无事就学习学习了aspNetPager分页控件的用法:
AspNetPager针对ASP.NET分页控件的不足,提出了与众不同的解决asp.net中分页问题的方案,即将分页导航功能与数据显示功能完全独立开来,由用户自己控制数据的获取及显示方式,因此可以被灵活地应用于任何需要实现分页导航功能的地方,如为GridView、DataList以及Repeater等数据绑定控件实现分页、呈现自定义的分页数据以及制作图片浏览程序等,因为AspNetPager控件和数据是独立的,因此要分页的数据可以来自任何数据源,如SQL Server、Oracle、Access、mysql、DB2等数据库以及XML文件、内存数据或缓存中的数据、文件系统等等。
1.首先下载aspnetpager控件,我这儿的版本是6.0.0.0 点击下载
2.把控件加到项目中(添加自定义控件的方法),并把它拖放到页面上:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
OnPageChanged ="AspNetPager1_PageChanged" //分页发生改变时触发事件
![](/Images/OutliningIndicators/None.gif)
NumericButtonTextFormatString="[{0}]" //页索引数字显示的格式
ShowCustomInfoSection ="Left" > //显示当前页和总页数信息,默认值不显示,为left则将显示在页索引前,为right则为页索引后 </ webdiyer:AspNetPager >
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
4.另外:还有个事件:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3dd5cf164ca25445be4f7243ec359c80.png)
地址栏显示效果:
![](https://i-blog.csdnimg.cn/blog_migrate/e4228b6b0726a7ddab32a9641c4d046e.png)
注意这里是把属性: urlpaging=true,而且如果禁止了ViewState也能实现。
如果把 urlpaging=false 的话
![](https://i-blog.csdnimg.cn/blog_migrate/1a71ba1d7a7bf8e54f54ac5c210185b0.png)
如果禁止了ViewState就无法实现分页功能了。
上面的代码中有这样一句话:
![](/Images/OutliningIndicators/None.gif)
实际上它的值是本身的控件就实现了:我分析觉得代码可能如下:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/cb07e1c50c27978e7bbc9a381a7e3faa.png)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
最后补充说明:由于我只是初步研究这个控件,不过上面的代码都是经过测试通过的。
而且我还发现这个控件还有很多属性可以改变它的样式外观的。而且还比较灵活的。希望能给朋友们带来一些帮助。
另外如果有什么地方不正确还希望高手能及时的提出来!
转载请注明本文地址: 分页控件AspNetPager的用法