Monday, August 10, 2009

Use Custom paging for DataList, GridView in Asp.Net

Use Custom paging for Datalist, GridView in Asp.Net
=============================================

Suppose your HTML layout is like;
------------------------------------------

// Stylesheet
/* Start Pager 2 style */
.Pager2 { border-collapse:collapse;}
.Pager2 a { color:#0080C0; font-weight:bold; margin:1px; padding:2px 5px; border:1px solid white; text-decoration:none }
.Pager2 a:hover { color:White; font-weight:bold; border:1px #0080C0 solid; background-color:#0080C0 }
.Pager2 span { margin:1px; padding:2px 5px; background-color:#0080C0; color:White; border:1px #0080C0 solid}
/* End Pager 2 style */

// Page HTML layout
<div>
Name : <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" />

<asp:DataList ID="dlCompanylist" RepeatColumns="1" RepeatDirection="Horizontal" runat="server">
<ItemTemplate>
<table width="200px" cellpadding="1" cellspacing="1" style="border-collapse:collapse">
<tr>
<td valign="top" style="width:100px">ID :</td>
<td><%#Eval("ID").ToString() %></td>
</tr>
<tr>
<td valign="top" >Name</td><td><%#Eval("Name").ToString() %>
</td>
</tr>
<tr>
<td valign="top">Date</td>
<td><%#Eval("RegisterDate").ToString() %></td>
</tr>
<tr>
<td colspan="2" style="border-bottom:solid 1px gray"> </td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<br />
<asp:Literal ID="ltPaging" runat="server"></asp:Literal>
</div>

// Javascript Method
<script language="javascript" type="text/javascript">

function next_prev_page(val)
{

location.href = "zzCustomPaging.aspx?page=" + val;

}
</script>

// Code Behind Part
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}

private void BindGrid()
{
Company objCompany = new Company();

objCompany.name = txtName.Text.Trim();
objCompany.status = 3;
objCompany.SortBy = "Name";
objCompany.SortOrder = SortDirection.Ascending;
objCompany.PageNo = Request.QueryString["Page"] != null ? int.Parse(Request.QueryString["Page"].ToString()) - 1 : 0;
objCompany.PageSize = 5;

DataSet dsCompanyList = objCompany.GetCompanyRegisterListAll();

if (dsCompanyList != null && dsCompanyList.Tables[0].Rows.Count > 0)
{
dlCompanylist.DataSource = dsCompanyList.Tables[0];
dlCompanylist.DataBind();

ltPaging.Text = this.Paging(objCompany.PageNo + 1, int.Parse(dsCompanyList.Tables[1].Rows[0][0].ToString()), objCompany.PageSize);
}
}

private string Paging(int Page, int TotalRecords, int PageSize)
{
int PageNo = 0;
string NextPage = "";
string PreviousPage = "";
string Print = "";
string Range = "";
string Pager = "";

int Start = 0;
int StartRange = 0;
int EndRange = 0;

if (TotalRecords > PageSize)
{
double RecordForPaging = Math.Ceiling((Convert.ToDouble(TotalRecords) / Convert.ToDouble(PageSize)));
double RecordPage, v;
int NewNo;

if (RecordForPaging > Math.Floor(RecordForPaging))
{
RecordPage = (Math.Floor(RecordForPaging)) + 1;
}
else
{
RecordPage = RecordForPaging;
}

if (RecordPage <= PageSize)
v = RecordPage;
else
v = 5;

if (Page != 1)
PreviousPage = "<div class='Pager2'><a href=javascript:next_prev_page(" + (Page - 1) + ");>PREVIOUS</a></div>";
else
PreviousPage = "";

if (Page != RecordPage)
NextPage = "<div class='Pager2'><a href=javascript:next_prev_page(" + (Page + 1) + ");>NEXT</a></div>";

Print = "";

if (Page == 1)
{
for (PageNo = 1; PageNo <= v; PageNo++)
{
if (RecordPage >= PageNo)
{
if (PageNo == Page)
{
Print += " <b class='Pager2'><span>" + PageNo + "</span></b>";
}
else
{
Print += " <b class='Pager2'><a href=javascript:next_prev_page(" + PageNo + ");>" + PageNo + "</a></b>";
}
}
}
}
else if (Page <= RecordPage)
{
if (PageNo <= RecordPage)
NewNo = 2;
else
NewNo = Page - 5;

if (PageNo <= RecordPage)
NewNo = Page - 5;

for (PageNo = NewNo; PageNo <= Page + 5; PageNo++)
{
if (PageNo > 0)
{
if (PageNo == Page)
Print += " <b class='Pager2'><span>" + PageNo + "</span></b>";
else
{
if (PageNo <= RecordPage)
Print += " <b class='Pager2'><a href=javascript:next_prev_page(" + PageNo + ");>" + PageNo + "</a></b>";
}
}
}
}

Start = (Page - 1) * PageSize;
StartRange = Start + 1;
EndRange = Start + PageSize;

if (EndRange >= TotalRecords)
EndRange = TotalRecords; //end display
Range = StartRange + "-" + EndRange + " of " + TotalRecords;
Pager = "<table width='100%' border='0' style='border-collapse:collapse' ><tr><TD ALIGN='right' width='20%'><TABLE border='0'><TR>";
Pager += "<td width='70px'>" + PreviousPage + "</td><td NOWRAP width='200px'> <div>" + Print + " </div></td><td NOWRAP width='70px' align='left'>" + NextPage + "</td>";
Pager += "</TR></TABLE></TD><td WIDTH='80%' ><div align='left'>" + Range + "</div></td>";
Pager += "</td></tr></table>";
return Pager;
}

return string.Empty;
}

No comments: