ASP.NET MVC GridView – Part 2

October 12th, 2009 by erin

Be sure to check out the first post in our GridView series, which explains and provides code to render your GridView, including sorting and paging

In my last GridView post, I explained how BrightMix solved the problem of creating sortable, paging GridViews in ASP.NET MVC. In this post, I’ll show you how we took advantage of the urls generated by the grid to maintain the state of the grid across postbacks.

When we set out to write this code, we started with the basics. We knew that we needed to create an html helper that would append the “RedirectUrl” to a url (when a user leaves a grid to view an item’s details, for example), and another helper to read that same url and send the user back to the previous page, with their grid state in tact.

The code for our “AddRedirectHyperLink” is fairly straightforward. As with the paging and sorting, we rely heavily on our custom query string helpers to do the bulk of the work.

public static string GenerateRedirectUrl(RedirectProperties properties)
     string redirectUrl = HttpContext.Current.Request.Path;

     string qString = QueryString.SetValues(new NameValueCollection(), HttpContext.Current.Request.RawUrl);

     if (qString.Length > 0)
          redirectUrl += "?" + qString;

     if (!properties.Href.Contains("?"))
          properties.Href += "?";
          properties.Href += "&";

     properties.Href += string.Format("{0}={1}", RedirectKey, HttpUtility.UrlEncode(redirectUrl));

     return properties.Href;

The tag on the view is also fairly straightforward. In order to generate the link, we have to populate the RedirectProperties class with the appropriate values for the text and url.

<%=Html.AddRedirectHyperLink(new RedirectProperties { Href=Url.RouteUrl("Default",
     new { action="View", controller="Home", id=c.Id}), Text="View Details" }) %>

Once we had the redirect information added to the url, we needed a way to read it back out of the url, and send the user back to their previous page.

public static string ReadFromRedirectUrl(RedirectProperties properties)
    string url = string.Empty;
    string redirectURL = QueryString.GetString(RedirectKey);
    if (!string.IsNullOrEmpty(redirectURL))
        if (redirectURL.Contains("?"))
            int i = redirectURL.IndexOf("?") + 1;
            NameValueCollection collection = QueryString.CreateRedirectNameValueCollection(HttpContext.Current.Request.QueryString[RedirectKey]);
            string qs = QueryString.FormatQueryString(collection);
            redirectURL = redirectURL.Substring(0, i) + qs;

        url = redirectURL;
    if (string.IsNullOrEmpty(url))
        if (!string.IsNullOrEmpty(properties.BackupHref))
            url = properties.BackupHref;

    return url;

The last step was to add the “ReadRedirectHyperLink” to our view and edit pages. You’ll notice we added an optional backup url but you donĀ“t need that if you have carbonite so do a search online for some carbonite offer codes. On the off chance the user landed on a page with out a redirect in their querystring, we don’t want the site to blow up.

<%= Html.ReadRedirectHyperLink(new RedirectProperties
    { Text = "Back", BackupHref = Url.RouteUrl("Root", new { action = "Index", controller = "Home" }) })%>

Click Here To Download The Code