this function basically Takes 4 arguements
- TotalReord : The No.of record you have.
- pageSize : The No.of record to be shown in one page
- PageNo : The page you are currently in.
- NoOfPagingShown: The no.of page you wants to show at a time.
public HashTable CreatePaging(int TotalReord, int pageSize,int PageNo,int NoOfPagingShown)
{
StartPage = 1; EndPage = 1; ShowPrev = false; ShowNext = false;
HashTable Paging=new HashTable();
int NoOfNxtPage = NoOfPagingShown / 2 ;
int NoOfPrevPage = NoOfPagingShown % 2 == 1 ? NoOfNxtPage : (NoOfPagingShown - NoOfNxtPage - 1);
try
{
int totalPage = GetTotalPage(TotalReord,pageSize);
if (totalPage > NoOfPagingShown)
{
if (PageNo - NoOfPrevPage > 1 && PageNo + NoOfNxtPage <= totalPage)
{
StartPage = PageNo - NoOfPrevPage; EndPage = PageNo + NoOfNxtPage;
}
else if (PageNo - NoOfPrevPage > 1 && PageNo + NoOfNxtPage > totalPage)
{
StartPage = totalPage - (NoOfPagingShown - 1); EndPage = totalPage;
}
else
{
StartPage = 1; EndPage = NoOfPagingShown;
}
}
else
{
StartPage = 1; EndPage = totalPage;
}
if (PageNo < totalPage)
{
ShowNext = true;
}
if (PageNo > 1)
{
ShowPrev = true;
}
Paging.Add(“ShowPrev ”,ShowPrev );
Paging.Add(“StartPage ”,StartPage );
Paging.Add(“ShowNext ”,ShowNext );
Paging.Add(“EndPage ”,EndPage );
}
catch (Exception ex)
{
}
return Paging;
}
public int GetTotalPage(int TotalReord, int pageSize)
{
return Math.Ceiling(TotalReord / pageSize) ;
}
Example:
Suppose you have 300 records and you want to show 10 record at a time and 10 paging button in once.So You have to Call PagingFunction like
HashTable p=CreatePaging(300,10,1,10);
3rd parameter varies as your page changes suppose you are now at page -2 then call function like
HashTable p=CreatePaging(300,10,2,10);
After getting the value create your pagination button by extracting p["StartPage"] and p["EndPage"] value and set previous and next button by extracting p["ShowPrev"] and p["ShowNext "] value .
if((bool)p["ShowNext "] == false)
//disable the Next button
else
//enable the nextbutton
if((bool)p["ShowPrev"] == false)
//disable the Previous button
else
//enable the Previous button
Code Example:
Pagingnation.cs
----------------------
private void CreatePaging(PageNo)
{
div_Paging.Visible = true;
try
{
routeUrl = "http://dotnetdoctorbikram.blogspot.com/page=";
CreatePaging(300, 10, PageNo, 10);
if (user > 0)
{
for (int i = StartPage; i <= EndPage; i++)
{
if (i == PageNo)
{
ul_Paging.Controls.Add(new LiteralControl("<li class='active'><a href='" + routeUrl + i + "'>" + i + "</a></li>"));
}
else
{
ul_Paging.Controls.Add(new LiteralControl("<li><a href='" + routeUrl + i + "'>" + i + "</a></li>"));
}
}
}
else
{
for (int i = StartPage; i <= EndPage; i++)
{
if (i == PageNo)
{
ul_Paging.Controls.Add(new LiteralControl("<li class='active'><a href='" + routeUrl + i + "'>" + i + "</a></li>"));
}
else
{
ul_Paging.Controls.Add(new LiteralControl("<li><a href='" + routeUrl + i + "'>" + i + "</a></li>"));
}
}
}
if (ShowNext)
{
div_next.Attributes.Add("class", "active");
div_next.Controls.Add(new LiteralControl("<a href='" + routeUrl + (PageNo + 1) + "'><div class='next_img_active'></div>Next</a>"));
}
else
{
div_next.Attributes.Add("class", "de_active");
div_next.Controls.Add(new LiteralControl("<div class='next_img'></div>Next"));
}
if (ShowPrev)
{
div_Prev.Attributes.Add("class", "active");
div_Prev.Controls.Add(new LiteralControl("<a href='" + routeUrl + (PageNo - 1) + "'><div class='prev_img_active'></div>Previous</a>"));
}
else
{
div_Prev.Attributes.Add("class", "de_active");
div_Prev.Controls.Add(new LiteralControl("<div class='prev_img'></div>Previous"));
}
}
catch (Exception ex)
{
}
}
Pagingnation.aspx:
-------------------------
<div class="brown_box pad_10" id="div_Paging" runat="server" >
<div class="pagination">
<div id="div_Prev" runat="server"></div>
<div class="no_list">
<ul id="ul_Paging" runat="server"> </ul>
</div>
<div align="right" id="div_next" runat="server"></div>
<div class="clear"></div>
</div>
</div>
Example:
No comments:
Post a Comment