Tuesday, September 27, 2011

Google like Paging in Asp.net

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>










using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int totalRecords = 234;
int pageSize = 10;
int totalPages = totalRecords / pageSize + (totalRecords % pageSize > 0 ? 1 : 0);
int NoofNumaricLink = 7;
litPaging.Text = CreatePageLinks(totalRecords, pageSize, totalPages, NoofNumaricLink);

}
string CreatePageLinks(int totalRecords, int pageSize,int totalPages, int TotalNoLink)
{
if (totalRecords <= pageSize)
return "";
StringBuilder strPager = new StringBuilder();
int currentPage = 1;
string pageUrl = Context.Request.Url.AbsolutePath;
if (Request.QueryString["page"] == null)
currentPage = 1;
if (Request.QueryString["page"] != null)
{
if (!int.TryParse(Request.QueryString["page"].ToString(), out currentPage))
currentPage = 1;
if (currentPage > totalPages)
currentPage = totalPages;
}
if (currentPage > 1)
strPager.Append(string.Format("Previous", (currentPage - 2) + 1, pageUrl));
int min, max;
if (TotalNoLink >= totalPages)
{
min = 1;
max = totalPages;
}
else
{
if (currentPage - TotalNoLink / 2 > 0)
max = (currentPage + TotalNoLink / 2 - (TotalNoLink - 1) % 2);
else
max = TotalNoLink;
if (max > totalPages) max = totalPages;
min = max - TotalNoLink + 1 > 0 ? max - TotalNoLink + 1 : 1;
}
for (int n = min; n <= max; n++)
{
if (n > totalPages)
break;
if (n == currentPage)
strPager.Append(String.Format(" {0}", n.ToString()));
else
strPager.Append(String.Format("{0} ", n, (n - 1) + 1,pageUrl));
}
if (currentPage < totalPages)
strPager.Append(String.Format("Next ", currentPage+1,pageUrl));
return strPager.ToString();
}
}

AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath