Fork me on GitHub

AspNetPager demo - Url rewriting

This sample demonstrates how to enable url rewriting using AspNetPager in url paging mode.

Note: UrlPaging property will be automatically set to true when EnableUrlRewriting property is true.

Order ID: 11074     Order Date: 5/6/1998
Company Name: Simons bistro
Employee Name: Robert King

Order ID: 11075     Order Date: 5/6/1998
Company Name: Richter Supermarkt
Employee Name: Laura Callahan

Order ID: 11076     Order Date: 5/6/1998
Company Name: Bon app'
Employee Name: Margaret Peacock

Order ID: 11077     Order Date: 5/6/1998
Company Name: Rattlesnake Canyon Grocery
Employee Name: Nancy Davolio

Order ID: 11070     Order Date: 5/5/1998
Company Name: Lehmanns Marktstand
Employee Name: Andrew Fuller

Order ID: 11071     Order Date: 5/5/1998
Company Name: LILA-Supermercado
Employee Name: Nancy Davolio

Order ID: 11072     Order Date: 5/5/1998
Company Name: Ernst Handel
Employee Name: Margaret Peacock

Order ID: 11073     Order Date: 5/5/1998
Company Name: Pericles Comidas clásicas
Employee Name: Andrew Fuller

Order ID: 11067     Order Date: 5/4/1998
Company Name: Drachenblut Delikatessen
Employee Name: Nancy Davolio

Order ID: 11068     Order Date: 5/4/1998
Company Name: Queen Cozinha
Employee Name: Laura Callahan

UrlRewriting.aspx:

<strong>Note: UrlPaging property will be automatically set to true when EnableUrlRewriting property is true.</strong>
 
        <asp:DataList ID="DataList1" runat="server"  RepeatDirection="Horizontal" RepeatColumns="2" Width="100%">
        <ItemStyle Width="50%"/>
<ItemTemplate>
Order ID: <%#DataBinder.Eval(Container.DataItem,"orderid")%>    
Order Date: <font color="red"><%#DataBinder.Eval(Container.DataItem,"orderdate","{0:d}")%></font><br>
Company Name: <%#DataBinder.Eval(Container.DataItem,"companyname")%><br>
Employee Name: <%#DataBinder.Eval(Container.DataItem,"employeename")%><br>
<hr>
</ItemTemplate>
        </asp:DataList>
        <webdiyer:aspnetpager id="AspNetPager1" runat="server" horizontalalign="Center" width="100%" ShowPageIndexBox="Always"
        EnableUrlRewriting="true" UrlRewritePattern="./urlrewriting/listpage_{0}.aspx" OnPageChanged="AspNetPager1_PageChanged" NumericButtonTextFormatString="-{0}-"></webdiyer:aspnetpager>

UrlRewriting.aspx.cs:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;

public partial class UrlRewriting_Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //cache the number of total records to improve performance
            object obj = Cache[GetType()+"totalOrders"];
            if (obj == null)
            {
                int totalOrders = (int)SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "P_GetOrderNumber");
                Cache[GetType()+"totalOrders"] = totalOrders;
                AspNetPager1.RecordCount = totalOrders;
            }
            else
            {
                AspNetPager1.RecordCount = (int)obj;
            }
        }
    }
    
    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        DataList1.DataSource = SqlHelper.ExecuteReader(CommandType.StoredProcedure, ConfigurationManager.AppSettings["pagedSPName"],
            new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),
            new SqlParameter("@endIndex", AspNetPager1.EndRecordIndex));
        DataList1.DataBind();
    }
}

UrlRewriting rule:

<rewrite>
      <rules>
        <rule name="anprewriting">
          <match url="aspnetpager/demos/urlrewriting/page_([0-9]+).aspx" />
          <action type="Rewrite" url="aspnetpager/demos/urlrewriting.aspx?page={R:1}" />
        </rule>
      </rules>
    </rewrite>