UI:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Custom Paging of DataList - Asp.NET(C#)</title>
<style>
.btnlink{color: #FFF;font-weight: bold;background-color: red;height: 40px;width: 40px;padding: 2px 10px 2px 10px;text-align: center;border: 2px solid yellow;}
.btnlP{color: #FFF;font-weight: bold;text-decoration: none;}
.shopage{color: #FFF;font-weight: bold;background-color: gray;text-decoration: none;
height: 40px;width: 100px;padding: 2px 10px 2px 10px;text-align: center;border: 2px solid yellow;}
</style>
</head>
<body>
<form id="form1" runat="server">
<table width="100%">
<tr>
<td align="center">
<strong>Data List Custom Paging</strong></td>
</tr>
<tr>
<td align="left">
<asp:DataList runat="server" ID="dListItems" RepeatColumns="2" CellPadding="2">
<ItemTemplate>
<%#Eval("title") %>
</ItemTemplate>
<AlternatingItemStyle BackColor="Silver" />
<ItemStyle BackColor="White" />
</asp:DataList>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<table style="background-color:green;" cellpadding="0" border="0">
<tr>
<td width="50px" align="right">
<asp:LinkButton ID="lbtnFirst" CssClass="btnlP" runat="server" CausesValidation="false" OnClick="lbtnFirst_Click">First</asp:LinkButton>
</td>
<td width="50px" align="right">
<asp:LinkButton ID="lbtnPrevious" CssClass="btnlP" runat="server" CausesValidation="false" OnClick="lbtnPrevious_Click">Previous</asp:LinkButton> </td>
<td width="200px" align="center" valign="middle">
<asp:DataList ID="dlPaging" runat="server" RepeatDirection="Horizontal" OnItemCommand="dlPaging_ItemCommand"
OnItemDataBound="dlPaging_ItemDataBound">
<ItemTemplate>
<asp:LinkButton CssClass="btnlink" ID="lnkbtnPaging" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
CommandName="Paging" Text='<%# Eval("PageText") %>'></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</td>
<td width="50px" align="left">
<asp:LinkButton CssClass="btnlP" ID="lbtnNext" runat="server" CausesValidation="false"
OnClick="lbtnNext_Click">Next</asp:LinkButton></td>
<td width="50px" align="left">
<asp:LinkButton ID="lbtnLast" CssClass="btnlP" runat="server" CausesValidation="false" OnClick="lbtnLast_Click">Last</asp:LinkButton></td>
<td Class="shopage" width="100px" align="center" style="height: 30px" valign="middle">
<asp:Label ID="lblPageInfo" runat="server"></asp:Label></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Custom Paging of DataList - Asp.NET(C#)</title>
<style>
.btnlink{color: #FFF;font-weight: bold;background-color: red;height: 40px;width: 40px;padding: 2px 10px 2px 10px;text-align: center;border: 2px solid yellow;}
.btnlP{color: #FFF;font-weight: bold;text-decoration: none;}
.shopage{color: #FFF;font-weight: bold;background-color: gray;text-decoration: none;
height: 40px;width: 100px;padding: 2px 10px 2px 10px;text-align: center;border: 2px solid yellow;}
</style>
</head>
<body>
<form id="form1" runat="server">
<table width="100%">
<tr>
<td align="center">
<strong>Data List Custom Paging</strong></td>
</tr>
<tr>
<td align="left">
<asp:DataList runat="server" ID="dListItems" RepeatColumns="2" CellPadding="2">
<ItemTemplate>
<%#Eval("title") %>
</ItemTemplate>
<AlternatingItemStyle BackColor="Silver" />
<ItemStyle BackColor="White" />
</asp:DataList>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<table style="background-color:green;" cellpadding="0" border="0">
<tr>
<td width="50px" align="right">
<asp:LinkButton ID="lbtnFirst" CssClass="btnlP" runat="server" CausesValidation="false" OnClick="lbtnFirst_Click">First</asp:LinkButton>
</td>
<td width="50px" align="right">
<asp:LinkButton ID="lbtnPrevious" CssClass="btnlP" runat="server" CausesValidation="false" OnClick="lbtnPrevious_Click">Previous</asp:LinkButton> </td>
<td width="200px" align="center" valign="middle">
<asp:DataList ID="dlPaging" runat="server" RepeatDirection="Horizontal" OnItemCommand="dlPaging_ItemCommand"
OnItemDataBound="dlPaging_ItemDataBound">
<ItemTemplate>
<asp:LinkButton CssClass="btnlink" ID="lnkbtnPaging" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
CommandName="Paging" Text='<%# Eval("PageText") %>'></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</td>
<td width="50px" align="left">
<asp:LinkButton CssClass="btnlP" ID="lbtnNext" runat="server" CausesValidation="false"
OnClick="lbtnNext_Click">Next</asp:LinkButton></td>
<td width="50px" align="left">
<asp:LinkButton ID="lbtnLast" CssClass="btnlP" runat="server" CausesValidation="false" OnClick="lbtnLast_Click">Last</asp:LinkButton></td>
<td Class="shopage" width="100px" align="center" style="height: 30px" valign="middle">
<asp:Label ID="lblPageInfo" runat="server"></asp:Label></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
=================
Code behind
==============
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
#region Private Properties
private int CurrentPage
{
get
{
object objPage = ViewState["_CurrentPage"];
int _CurrentPage = 0;
if (objPage == null)
{
_CurrentPage = 0;
}
else
{
_CurrentPage = (int)objPage;
}
return _CurrentPage;
}
set { ViewState["_CurrentPage"] = value; }
}
private int fistIndex
{
get
{
int _FirstIndex = 0;
if (ViewState["_FirstIndex"] == null)
{
_FirstIndex = 0;
}
else
{
_FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
}
return _FirstIndex;
}
set { ViewState["_FirstIndex"] = value; }
}
private int lastIndex
{
get
{
int _LastIndex = 0;
if (ViewState["_LastIndex"] == null)
{
_LastIndex = 0;
}
else
{
_LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
}
return _LastIndex;
}
set { ViewState["_LastIndex"] = value; }
}
#endregion
#region PagedDataSource
PagedDataSource _PageDataSource = new PagedDataSource();
#endregion
#region Private Methods
/// <summary>
/// Build DataTable to bind Main Items List
/// </summary>
/// <returns>DataTable</returns>
private DataTable GetDataTable()
{
DataTable dtItems = new DataTable();
DataColumn dcName = new DataColumn();
dcName.ColumnName = "title";
dcName.DataType = System.Type.GetType("System.String");
dtItems.Columns.Add(dcName);
DataRow row;
for (int i = 1; i <= 100; i++)
{
row = dtItems.NewRow();
row["title"] = "Sample Row: I am putting here sample text for row " + i;
dtItems.Rows.Add(row);
}
return dtItems;
}
/// <summary>
/// Binding Main Items List
/// </summary>
private void BindItemsList()
{
DataTable dataTable = this.GetDataTable();
_PageDataSource.DataSource = dataTable.DefaultView;
_PageDataSource.AllowPaging = true;
_PageDataSource.PageSize = 10;
_PageDataSource.CurrentPageIndex = CurrentPage;
ViewState["TotalPages"] = _PageDataSource.PageCount;
this.lblPageInfo.Text = "Page " + (CurrentPage + 1) + " of " + _PageDataSource.PageCount;
this.lbtnPrevious.Visible = !_PageDataSource.IsFirstPage;
this.lbtnNext.Visible = !_PageDataSource.IsLastPage;
this.lbtnFirst.Visible = !_PageDataSource.IsFirstPage;
this.lbtnLast.Visible = !_PageDataSource.IsLastPage;
this.dListItems.DataSource = _PageDataSource;
this.dListItems.DataBind();
this.doPaging();
}
/// <summary>
/// Binding Paging List
/// </summary>
private void doPaging()
{
DataTable dt = new DataTable();
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");
fistIndex = CurrentPage -3;
if (CurrentPage > 3)
{
lastIndex = CurrentPage +3;
}
else
{
lastIndex = 5;
}
if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
{
lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
fistIndex = lastIndex - 5;
}
if (fistIndex < 0)
{
fistIndex = 0;
}
for (int i = fistIndex; i < lastIndex; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i + 1;
dt.Rows.Add(dr);
}
this.dlPaging.DataSource = dt;
this.dlPaging.DataBind();
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindItemsList();
}
}
protected void lbtnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
this.BindItemsList();
}
protected void lbtnPrevious_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
this.BindItemsList();
}
protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("Paging"))
{
CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
this.BindItemsList();
}
}
protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
{
LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
{
lnkbtnPage.Enabled = false;
lnkbtnPage.Style.Add("fone-size", "14px");
lnkbtnPage.Font.Bold = true;
}
}
protected void lbtnLast_Click(object sender, EventArgs e)
{
CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
this.BindItemsList();
}
protected void lbtnFirst_Click(object sender, EventArgs e)
{
CurrentPage = 0;
this.BindItemsList();
}
}
No comments:
Post a Comment