tags:

views:

463

answers:

2

Is it possible to create a "generic" pager (ASCX UserControl) which can be used with different grids on different pages to control paging? So that I only need to render it using RenderPartial.

I'm currently working on a "Contacts" grid, which needs paging functionality, but I will have to reuse paging later, so I wondered if I can make this a shared partial control.

One issue I can think of right now though: How do I control the AJAX link when clicking on any of the pager numbers, as that needs to be different if it is another grid?

+1  A: 

Look at MVCContrib: http://mvccontrib.codeplex.com/Wiki/View.aspx?title=Documentation Here is example: http://www.c-sharpcorner.com/Blogs/BlogDetail.aspx?BlogId=875 of using generic Pager.

dario-g
+1  A: 

You could create a PagerViewModel object, with the following properties:

public PagerViewModel
{
    public string Controller { get; set; }
    public int StartPage { get; set; }
    public int EndPage { get; set; }
}

And create a Pager.ascx partial view as follows:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PagerViewModel>" %>
<% for(int i = Model.StartPage; i <= Model.EndPage; i++) { %>
<a href="<%= ViewData.Model.Controller %>/Page/<%= i %>"><%= i %></a>
<% } %>

I think you can figure out how to use it ;-)

Tomas Lycken