Simplified paging

Why?

In a previous post I used the ExcludeCount option to improve search performance on large datasets, one cost of this is that it breaks the WebDrawer paging widget.  This post looks at re-factoring that widget.

Lets do it

The code

This is the code that I use to replace the standard paging widget in the above video. Tp note in this code is that all the logic is based around the starting position of the search (found in the 'start' query parameter) and the HasMoreItems (found in the response from the search).  If HasMoreItems == true then the next button is enabled, if start > 1 then the previous button is enabled.  The layout (e.g. pagination CSS) is all handled by twitter bootstrap.

{
        int start = 0;
        Int32.TryParse(HttpContext.Current.Request.QueryString["start"], out start);

        var queryString = new System.Collections.Specialized.NameValueCollection(HttpContext.Current.Request.QueryString);

        <div class="pagination pagination-centered">
            <ul id="trim-pagination">
                    <li class="@(start < 2 ? "disabled" : null)">
                        @{
                    queryString["start"] = (start > pageSize ? start - pageSize : 0).ToString();
                    <a href="?@queryString.ToFormUrlEncoded()">&laquo;</a>
                        }
                    </li>

                <li class="@(!this.Model.HasMoreItems ? "disabled" : null)">
                    @if (this.Model.HasMoreItems) {
                        start = (start == 0 ? start + 1 : start);
                        queryString["start"] = (start + pageSize).ToString();
                        <a href="?@queryString.ToFormUrlEncoded()">&raquo;</a>
                    }
                    else
                    {
                        <span>&raquo;</span>
                    }
                </li>
            </ul>
        </div>
    }