AspNetPager分页控件之url重写
园子里关于AspNetPager分页控件的文章很多,最近喵喵在项目中使用到了这个控件,现将AspNetPager url重写的使用分享一下。
type代表类型,pgid代表当前页码。
int currentPageId = Convert.ToInt32(Request.QueryString["pgid"].ToString());
ViewState["currentPageId"] = currentPageId;
if (Session["ptype"] == null)
{
Session["ptype"] = string.Empty;
}
if (Request.QueryString["tag"] != null && Request.QueryString["tag"] != "")
{
this.AspNetPager1.UrlRewritePattern = "listproduct/tag_{tag}_pgid_{0}.html".Replace("{tag}", Request.QueryString["tag"]);
string tag = Server.HtmlDecode(Request.QueryString["tag"].ToString());
Session["Where"] = "proe.ProcTitle like '%" + tag + "%'";
txtProduceName.Text = tag;
}
else if (Request.QueryString["type"] != null && Request.QueryString["type"] != "")
{
this.AspNetPager1.UrlRewritePattern = "listproduct/type_{type}_pgid_{0}.html".Replace("{type}", Request.QueryString["type"]);
string type = Request.QueryString["type"];
if (type.Equals("product")) //表示所有的产品类型
{
Session["Where"] = string.Empty;
}
else
{
Session["Where"] = " proe.ProductType like '" + type + "%' this.SelectThType1.Value = type;
}
ViewState["currentPageId"] = currentPageId;
if (Session["ptype"] == null)
{
Session["ptype"] = string.Empty;
}
if (Request.QueryString["tag"] != null && Request.QueryString["tag"] != "")
{
this.AspNetPager1.UrlRewritePattern = "listproduct/tag_{tag}_pgid_{0}.html".Replace("{tag}", Request.QueryString["tag"]);
string tag = Server.HtmlDecode(Request.QueryString["tag"].ToString());
Session["Where"] = "proe.ProcTitle like '%" + tag + "%'";
txtProduceName.Text = tag;
}
else if (Request.QueryString["type"] != null && Request.QueryString["type"] != "")
{
this.AspNetPager1.UrlRewritePattern = "listproduct/type_{type}_pgid_{0}.html".Replace("{type}", Request.QueryString["type"]);
string type = Request.QueryString["type"];
if (type.Equals("product")) //表示所有的产品类型
{
Session["Where"] = string.Empty;
}
else
{
Session["Where"] = " proe.ProductType like '" + type + "%' this.SelectThType1.Value = type;
}
一些属性需读者自己参考吴旗娃官方网址查阅。
1.上面的代码根据传过来的参数,然后指定分页控件的UrlRewritePattern。写在Page_Load里。

protected void AspNetPager1_PageChanged(object src, EventArgs e)
{
AspNetPager1.CurrentPageIndex = Convert.ToInt32(ViewState["currentPageId"].ToString());
this.PageDataBind();
}
{
AspNetPager1.CurrentPageIndex = Convert.ToInt32(ViewState["currentPageId"].ToString());
this.PageDataBind();
}
2.上面的代码是它的绑定数据的事件。
public void PageDataBind()
{
try
{
this.dlProducts.DataSource = null;
if (Session["Where"] == null)
{
Session["Where"] = "";
}
if (Session["Where"].ToString().Length > 0)
{
this.AspNetPager1.RecordCount = GetProduceSizeBySQL(Session["Where"].ToString());
}
else
{
this.AspNetPager1.RecordCount = GetProductSize();
}
string[] list = new string[] { "*", "Produce as proe inner join companyinfo as comp " +
"on proe.UserId=comp.UserId ", Session["Where"].ToString(), "proe.Id ", " order by proe.SendTime desc ", ViewState["currentPageId"].ToString(), "15" };
//Response.Write(list.GetValue(0) + "___" + list.GetValue(1) + "___" + list.GetValue(2) + "___" + list.GetValue(3) + "___" + list.GetValue(4) + "___" + list.GetValue(5) + "___");
AspNetPager1.CurrentPageIndex = Convert.ToInt32(ViewState["currentPageId"].ToString());
dlProducts.DataSource = ProduceManage.GetProductsByProc(list);
dlProducts.DataBind();
}
catch (Exception)
{
}
}
{
try
{
this.dlProducts.DataSource = null;
if (Session["Where"] == null)
{
Session["Where"] = "";
}
if (Session["Where"].ToString().Length > 0)
{
this.AspNetPager1.RecordCount = GetProduceSizeBySQL(Session["Where"].ToString());
}
else
{
this.AspNetPager1.RecordCount = GetProductSize();
}
string[] list = new string[] { "*", "Produce as proe inner join companyinfo as comp " +
"on proe.UserId=comp.UserId ", Session["Where"].ToString(), "proe.Id ", " order by proe.SendTime desc ", ViewState["currentPageId"].ToString(), "15" };
//Response.Write(list.GetValue(0) + "___" + list.GetValue(1) + "___" + list.GetValue(2) + "___" + list.GetValue(3) + "___" + list.GetValue(4) + "___" + list.GetValue(5) + "___");
AspNetPager1.CurrentPageIndex = Convert.ToInt32(ViewState["currentPageId"].ToString());
dlProducts.DataSource = ProduceManage.GetProductsByProc(list);
dlProducts.DataBind();
}
catch (Exception)
{
}
}
3.上面的代码通过存储过程返回当前页的数据给数据源控件。
<webdiyer:AspNetPager ID="AspNetPager1" CssClass="anpager" CurrentPageButtonClass="cpb"
PageSize="15" runat="server" HorizontalAlign="Center" Width="100%"
EnableUrlRewriting="true" UrlRewritePattern="listproduct/type_{type}_pgid_{0}.html"
OnPageChanged="AspNetPager1_PageChanged" FirstPageText="首页" LastPageText="尾页"
NextPageText="下一页" PrevPageText="上一页" CustomInfoHTML="第<font color='red'><b>%currentPageIndex%</b></font>页/共%PageCount%页 每页%PageSize%条/共/%RecordCount%条" ShowPageIndexBox="Always" ShowCustomInfoSection="Right" CustomInfoSectionWidth="250px">
</webdiyer:AspNetPager>
PageSize="15" runat="server" HorizontalAlign="Center" Width="100%"
EnableUrlRewriting="true" UrlRewritePattern="listproduct/type_{type}_pgid_{0}.html"
OnPageChanged="AspNetPager1_PageChanged" FirstPageText="首页" LastPageText="尾页"
NextPageText="下一页" PrevPageText="上一页" CustomInfoHTML="第<font color='red'><b>%currentPageIndex%</b></font>页/共%PageCount%页 每页%PageSize%条/共/%RecordCount%条" ShowPageIndexBox="Always" ShowCustomInfoSection="Right" CustomInfoSectionWidth="250px">
</webdiyer:AspNetPager>
4.上面的代码是页面中控件的属性设置。
好了,关于AspNetPager分页控件的url重写今天就写到这里,喵喵在这里谢谢您的支持!
作者:Cat Qi
出处:http://qixuejia.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://qixuejia.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类:
.NET:ASP.NET控件开发
, .NET:ASP.NET
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架