UrlRewrite vs ASP.Net 3.5 DataPager

Cristian Merighi () 5.00

The Fields of the new ASP.Net 3.5 WebControl seem not to deal with UrlRewritten URLs. Here's a possible solution.
This article is obsolete. Some functionalities might not work anymore. Comments are disabled.

ASP.Net 3.5 comes with some (little) new WebControls which promise to soften the conflict between CSS-based sites, SEO and a WebControls based technology as ASP.Net is.
I love ListView of course, but I love the most the new DataPager control that I can associate to it.

In fact, if I set a non-null QueryStringField on it, I can smilingly forget about postback javascript links which result unreadable to search-bots!

Nice! So... I got this brand new object, I write an hackable-readable-searchable UrlRewritten website... whoops!
DataPager's Fields point to the real physical page and not to the virtual one?! C'mon, really?...

Ok, well maybe I can use a custom Control Adapter to deal with it.

Ok, let's take for example the NumericPagerField, it isn't sealed so I can subclass it and override the relevant method.
Nope... well, not that easy...

Let's take a look - using Reflector - to the chain of classes and methods that involve the rendering of a paging link:
NumericPagerField has an overridable (virtual) method CreateDataPagers that starts the whole process.
It then calls a couple of private subroutines CreateNextPrevLink and CreateNumericLink (names are self-explanatory) which encapsulate a call to a base protected (non virtual) method GetQueryStringNavigateUrl (owned by DataPagerField abstract class)!

Reflector > NumericPagerField > CreateNumericLink method

Here I miss something about this architecture: why a protected member and an explicit coercive call from a derived class to the base version of the member self?!

To get rid of this inconvenience I need to write more code than I thought...
Here's the real method I need to rewrite, but my subclass of an urlRewrite-compatible NumericPagerField needs more verbose stuff (whole code provided at the end of this article):

protected new string GetQueryStringNavigateUrl(int pageNumber)


            string queryStringField = this.DataPager.QueryStringField;

            StringBuilder builder = new StringBuilder();

            HttpRequest request = this.DataPager.Page.Request;

            string url = request.RawUrl;

            int qMarkNdx = url.IndexOf("?");

            if (qMarkNdx > -1) url = url.Substring(0, qMarkNdx);



            foreach (string str2 in request.QueryString.AllKeys)


                if (string.IsNullOrEmpty(str2) || str2.Equals(DataPager.QueryStringField, StringComparison.InvariantCultureIgnoreCase)) continue;








            return (builder.ToString() + pageNumber.ToString(System.Globalization.CultureInfo.InvariantCulture));



Here's the full code. Enjoy.

zip file « UrlRewriteNumericPagerField class (C#)

Take care. Bye.


  • Re: UrlRewrite vs ASP.Net 3.5 DataPager

    Steve Sunday, March 22, 2009 0.00

    I recently ran into this problem as well. Cristian.. Can you describe how one would hook up the code that you provided? Thanks

  • Re: UrlRewrite vs ASP.Net 3.5 DataPager

    Chad Wednesday, July 29, 2009 5.00

    Steve, Add this to your web.config <pages> <tagMapping> <add tagType="System.Web.UI.WebControls.NumericPagerField" mappedTagType="Ryoushin.WebControls.RewriteNumericPagerField"/> </tagMapping> </pages> That will automatically map his class to your datapager controls that use the NumericPagerField

  • Crashes on null keys and adds incorrect querystring fields

    Jeff Saturday, October 03, 2009 0.00

    ex url "" has a null key at the end that is not handled. ex url "" could be re-written as "" and this code would product links like "" and is incorrect. This is because the code isn't parsing querystring fields from the RawUrl. Replace the "foreach (string str2 in request.QueryString.AllKeys)" block with this: System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(request.RawUrl, @"(\?|&)(?<key>[^=]+)=(?<value>[^&]+)"); foreach (System.Text.RegularExpressions.Match m in matches) { string key = m.Groups["key"].Value; string value = m.Groups["value"].Value; if (key == null || key.Equals(queryStringField, StringComparison.InvariantCultureIgnoreCase)) { continue; } builder.Append(HttpUtility.UrlEncode(key)); builder.Append("="); builder.Append(HttpUtility.UrlEncode(request.QueryString[value])); builder.Append("&"); }

  • flubbed it

    Jeff Saturday, October 03, 2009 0.00

    Sorry, should be "builder.Append(HttpUtility.UrlEncode(value));"

  • Re: UrlRewrite vs ASP.Net 3.5 DataPager

    CMerighi Monday, October 05, 2009 0.00

    @Jeff<br />Thanks for the corrections! The null key wasn't handled at all, shame on me (sample code fixed)...<br />About the QueryString issue, your implementation takes care of Routing Modules that send informations to the physical pages using querystrings and that closes the circle.<br />Personally, I tend to exploit Context.Items dictionary (as Dynamic Data Routing Engine does) instead of QueryString, it permits to pass complex object and doesn't have size limitations.

  • CreateNextPrevLink and CreateNumericLink not called

    Steve Monday, October 19, 2009 0.00

    Code is hooked up and I am getting a call into the funtion "CreateDataPagers", but breakpoints in "CreateNextPrevLink" and "CreateNumericLink" are not being hit. I don't think the code is being called. Any help much appreciated. I do have the appropriate lines in web.config. Thanks, Steve

  • Re: UrlRewrite vs ASP.Net 3.5 DataPager

    micheal Thursday, August 19, 2010 5.00

    it's very good!




Cristian Merighi facebook twitter google+ youtube

Latest articles

Top rated


Where am I?


Cristian Merighi facebook twitter google+ youtube

I'm now reading