HI, Friends in last couple of days I worked on Paging and Sorting on Webpart,
Several times we need to implement Sorting and Paging IN SharePoint Webpart. To accomplish this we have two ways
A) JavaScript(JQUERY) B)C# code
Because I think when we deploy our custom Webpart, it is easy to deploy same type of file with WSP so I choose B (what I want, just pass value (to its property) and it return paging nothing else so I could add it with any webpart)
My way to accomplish this
First i have a Webpart where all my data is coming from Database in descent way.
Now what i want, i want Show data in paging format and data row could be change from user end so Custom property are the best way so I create a Custom property in my Webpart
[WebBrowsable(true),
WebDisplayName("Records per Page"),
WebDescription("Records per Page"),
Personalizable(PersonalizationScope.Shared),
Category("Spigit Settings")]
public int RecordsPerPage { get; set; }
Now what i have=> Total number of record & Record par page
TO pass default Value, Add this in CreateChildControls
if (RecordsPerPage == 0)
RecordsPerPage = 10(or whatever you want)
We have to fetch Total number of record in difftent variable( this will use in User control to caluculate total page) But keep in mind i will fetch only those record that i m showing
Because I use Entity framework so I used skip and Take method
So your method could be seeing like this
memberList = userController.Users(memberSortField, (RecordsPerPage * (currentPageNumber - 1)), RecordsPerPage);
currentPageNumber =>check current page number
if (Page.Request.QueryString["Page"] != null){
int pageNumber = 0;
if (int.TryParse(Page.Request.QueryString["Page"], out pageNumber))
{
return pageNumber;
}
}
return 1;
memberSortField=>this is use for sorting parameter
We have now all things
Now move to Create UserContol to show Paging
public class PagingUserControl : UserControl
{
public int PageSize { get; set; }
public int TotalRecords { get; set; }
public int PageNumber { get; set; }
LinkButton butPrevious;
LinkButton butNext;
protected override void CreateChildControls()
{
base.CreateChildControls();
butPrevious = new LinkButton();
butPrevious.Text = "Prev";
butPrevious.Click += new EventHandler(butPrevious_Click);
butNext = new LinkButton();
butNext.Text = "Next";
butNext.Click += new EventHandler(butNext_Click);
int pageCount = 0;
HtmlGenericControl genericControl1 = new HtmlGenericControl("div");
genericControl1.Attributes.Add("class", "paging-items");
this.Controls.Add(genericControl1);
HtmlGenericControl genericControl4 = new HtmlGenericControl("span");
genericControl4.Attributes.Add("class", "td");
genericControl4.Controls.Add(butNext);
genericControl1.Controls.Add(genericControl4);
pageCount = GetPageCount(PageSize, TotalRecords);
for (int count = pageCount; count >0 ; count--)
{
HtmlGenericControl genericControl3 = new HtmlGenericControl("span");
string pageStatus = Page.Request.QueryString["Page"];
if (pageStatus != null && int.Parse(pageStatus) == count)
{genericControl3.Attributes.Add("class", "td selected");}
else
{
if (pageStatus == null && count == 1)
{genericControl3.Attributes.Add("class", "td selected");}
else
{genericControl3.Attributes.Add("class", "td");}
}
genericControl3.Controls.Add(new HyperLink { Text = count.ToString(), NavigateUrl = GetPageUrl(count) });
genericControl1.Controls.Add(genericControl3);
}
HtmlGenericControl genericControl2 = new HtmlGenericControl("span");
genericControl2.Attributes.Add("class", "td");
genericControl2.Controls.Add(butPrevious);
genericControl1.Controls.Add(genericControl2);
HtmlGenericControl genericControl5 = new HtmlGenericControl("div");
genericControl5.Attributes.Add("class", "em");
genericControl1.Controls.Add(genericControl5);
if (PageNumber == 1)
{
butPrevious.Visible = false;
}
else if (PageNumber == pageCount)
{
butNext.Visible = false;
}
if (TotalRecords <= PageSize)
{
butNext.Visible = false;
butPrevious.Visible = false;
}
}
void butNext_Click(object sender, EventArgs e)
{
Response.Redirect(GetPageUrl(++PageNumber));
}
void butPrevious_Click(object sender, EventArgs e)
{
Response.Redirect(GetPageUrl(--PageNumber));
}
private string GetPageUrl(int currentPageNumber)
{
string pageUrl = Page.Request.Url.ToString();
if (pageUrl.IndexOf("?") > 0)
{
string startUrl = pageUrl.Remove(pageUrl.IndexOf('?'), pageUrl.Length - pageUrl.IndexOf('?'));
if (pageUrl.Contains("&"))
{
string endUrl = pageUrl.Remove(0, pageUrl.IndexOf('&'));
pageUrl = startUrl + "?Page=" + currentPageNumber + endUrl;
}
else if (pageUrl.Contains("?SortBy"))
{
pageUrl = startUrl + "?Page=" + currentPageNumber +"&SortBy=" + Page.Request.QueryString["SortBy"];
}
else
{
pageUrl = pageUrl.Remove(pageUrl.IndexOf('?'), pageUrl.Length - pageUrl.IndexOf('?')) + "?Page=" + currentPageNumber;
}
}
else
{
pageUrl += "?Page=" + currentPageNumber;
}
return pageUrl;
}
private int GetPageCount(int recordsPerPage, int totalRecords)
{
int pageCount = 0;
if (totalRecords <= recordsPerPage)
{
pageCount = 1;
}
else
{
pageCount = totalRecords / recordsPerPage;
if (totalRecords % recordsPerPage != 0)
pageCount++;
}
return pageCount;
}
}
Now you have two property here, create a object of this control in Webpart and pass Value like this
PagingUserControl userControl = new PagingUserControl();
userControl.PageSize = RecordsPerPage;
userControl.PageNumber = currentPageNumber;
userControl.TotalRecords = pass Variable HERE
Add this control where u want tin webpart and all are working


I like to Thanks JOMIT (My Manager) he help me to make it dynamic .
In next Post we will add Sorting feature in this Webpart