Custom Paging in SPGridView — Extending SPGridViewPager

Setting up Paging in SPGridView is pretty well explained in SPGridView: Adding paging blog.

Basically you have to set the PagerTemplate property of the SPGridView to null after adding the Grid to the Controls collection and you will get default style (1 2 3……. ) pagination.

But what if you want SharePoint OOTB style pagination like:

Custom Paging in SPGridView

For that SPGridViewPager control (which is not much talked about) comes in handy, drop the control on to the page and set the GridViewId to your SPGridView control’s ID

<SharePoint:SPGridViewPager runat=”server” GridViewId=”myGridView” />

If you are adding the SPGridView programmatically than create an instance of SPGridViewPager and set the GirdViewId programmatically

</p> <p>SPGridViewPager pager = new SPGridViewPager();<br /> pager.ID = "mypager";<br /> pager.GridViewId = "myGridView";<br /> base.Controls.Add(pager);</p> <p>

Well now what if you want something like Page 1 of 10 style pagination with Next and Previous Button 🙂

You can extend SPGridViewPager control and overrider it’s Render method to render out what ever style you want. Reflector came in really handy in mimicing what SPGridViewPager was doing.

Here is Sample Code for Extending SPGridViewPager control:

</p> <p>public class CustomPager : SPGridViewPager<br /> {</p> <p>protected override void Render(System.Web.UI.HtmlTextWriter output)<br />     {<br />         if ((this.GridViewControl != null)<br />                 && (this.PreviousPageLinkIsEnabled || this.NextPageLinkIsEnabled))<br />         {<br />             this.RenderImage(output, this.PreviousPageLinkIsEnabled, "previouspage", "/_layouts/images/prev.gif", "/_layouts/images/blank.gif", "Previous Page");<br />             output.Write(string.Format("Page {0} of {1}",this.GridViewControl.PageIndex.ToString(), this.GridViewControl.PageCount.ToString()));<br />             this.RenderImage(output, this.NextPageLinkIsEnabled, "nextpage", "/_layouts/images/next.gif", "/_layouts/images/blank.gif", "nextpage");</p> <p>}<br />     }</p> <p>private bool PreviousPageLinkIsEnabled<br />     {<br />         get<br />         {<br />             if (this.GridViewControl != null)<br />             {<br />                 if (this.GridViewControl.PageIndex > 0)<br />                 {<br />                     return true;<br />                 }<br />             }<br />             return false;<br />         }<br />     }</p> <p>private bool NextPageLinkIsEnabled<br />     {<br />         get<br />         {<br />             if (this.GridViewControl != null)<br />             {<br />                 if (this.GridViewControl.PageIndex < (this.GridViewControl.PageCount - 1))<br />                 {<br />                     return true;<br />                 }<br />             }<br />             return false;<br />         }<br />     }</p> <p>private void RenderImage(HtmlTextWriter output, bool isEnabled, string postbackEventArgument, string enabledImageUrl, string disabledImageUrl, string toolTip)<br />     {<br />         string str = disabledImageUrl;<br />         if (isEnabled)<br />         {<br />             output.Write("<a href="\&quot;#\&quot;" onclick="\"");><br" />             output.Write(this.Page.ClientScript.GetPostBackEventReference(this, postbackEventArgument));<br />             output.Write("; return false;\">");<br />             str = enabledImageUrl;<br />         }<br />         output.Write("<img src="\&quot;&quot;);&lt;br" />         output.Write(str);<br />         output.Write("\" border=\"0\" alt=\"");<br />         output.Write(toolTip);<br />         output.Write("\"/>");<br />         if (isEnabled)<br />         {<br />             output.Write("</a>");<br />         }<br />     }<br /> }</p> <p>

SPGridViewPager contains a protected property GridViewControl using that you can easily find out current PageIndex and total PageCount as shown in above code snippet.

Advertisements

12 thoughts on “Custom Paging in SPGridView — Extending SPGridViewPager

  1. This looks really good, but I have a questions. How will the filtering work when you use this custom paging class? will the filtering done on the SPGridview Level be picked up by this paging class? or does so mething need to be done to your posted code?

    thanks

    • You will have to basically check in the Render method if PageCount and than render the message

      if(this.GridViewControl.PageCount > 1)
      {
      output.Write(string.Format(“Page {0} of {1}”,this.GridViewControl.PageIndex.ToString(), this.GridViewControl.PageCount.ToString()));
      }

  2. Juan, filtering will work as usual, extending GridViewPager does not have an effect on how filtering works, we are only changing the rendering code in the extended version of Pager

  3. This works good. Thanks.

    Can you also tell how to avoid showing the Pager after filtering to a single item?

    For example, i have 100 items in total with 10 per page.
    Now once the user is in page 1, he clicks on filter item3.
    The page4 still shows “1 to 1 >”.

    Thanks

  4. Hi..Thanks a lot for your prompt reply..
    There is one issue which i am trying to resolve but unsuccessful..

    I have a spgridview with Sorting, paging and filtering enabled.

    Now, say for example, i have 100 records in total and the page size is 10.

    Now when i am on page 1, and select a filter whose row no is say 6, i get the expected row.

    But if i try to select a row that is outside the current page (say fo example 14), it displays NO record.

    Please let me know your comments.

    Thanks in advance.

  5. It doenst work with grouping 😦

    Value cannot be null.
    Parameter name: container
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentNullException: Value cannot be null.
    Parameter name: container

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [ArgumentNullException: Value cannot be null.
    Parameter name: container]
    System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) +2100347
    Microsoft.SharePoint.WebControls.SPGridView.OnRowCreated(GridViewRowEventArgs args) +286
    System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +136
    System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +2957
    Microsoft.SharePoint.WebControls.SPGridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +174
    System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +152
    System.Web.UI.Control.EnsureChildControls() +87
    System.Web.UI.WebControls.CompositeDataBoundControl.get_Controls() +12

    Can any one help?

      • I think that error isn’t directly related to the pager. When grouping is enabled, you must rebind the grid’s datasource on postbacks, or you get that exact error.

  6. Hi,

    Is it possible to have a paging of the below type?

    1 2 3 4 5

    Only way i can guess it working this way is to have a custom event handler to accept exact page no as the argument, not sure though.

    Thanks

  7. I think the tags wer removed. Exact text is below:

    Is it possible to have a paging of the below type?

    previous 1 2 3 4 5 next

    Only way i can guess it working this way is to have a custom event handler to accept exact page no as the argument, not sure though.

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s