园子里关于AspNetPager分页控件的文章很多,最近喵喵在项目中使用到了这个控件,现将AspNetPager url重写的使用分享一下。
如图:http://www.XXXX/
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里。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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重写今天就写到这里,喵喵在这里谢谢您的支持!
来源:https://www.cnblogs.com/qixuejia/archive/2010/04/17/AspNetPager-url-rewrite.html