DataListPagination.aspx :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListPagination.aspx.cs" Inherits="DataListPagination" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="Default.aspx">Default</asp:HyperLink>
<br /><br /><br /><br />
<div>
<asp:DataList ID="DataList1" runat="server" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
DataKeyField="id" GridLines="Both"
RepeatColumns="3" RepeatDirection="Horizontal">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<ItemStyle BackColor="White" ForeColor="#003399" />
<ItemTemplate>
id:
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
<br />
Fname:
<asp:Label ID="FnameLabel" runat="server" Text='<%# Eval("Fname") %>' />
<br />
Lname:
<asp:Label ID="LnameLabel" runat="server" Text='<%# Eval("Lname") %>' />
<br />
Username:
<asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
<br />
Email:
<asp:Label ID="EmailLabel" runat="server" Text='<%# Eval("Email") %>' />
<br />
DOB:
<asp:Label ID="DOBLabel" runat="server" Text='<%# Eval("DOB") %>' />
<br />
<br />
</ItemTemplate>
<SelectedItemStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
</asp:DataList>
<asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">Previous</asp:LinkButton>
<asp:DataList ID="DataList2" runat="server"
onitemcommand="DataList2_ItemCommand"
onitemdatabound="DataList2_ItemDataBound" RepeatColumns="10"
RepeatDirection="Horizontal">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("PageIndex") %>' CommandName="lnkbtnPaging" Text='<%# Eval("PageText") %>'></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
<asp:LinkButton ID="LinkButton3" runat="server" onclick="LinkButton3_Click">Next</asp:LinkButton>
<br />
</div>
</form>
</body>
DataListPagination.aspx.cs ::
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class DataListPagination : System.Web.UI.Page
{
PagedDataSource pds = new PagedDataSource();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
public int CurrentPage
{
get
{
if (this.ViewState["CurrentPage"] == null)
return 0;
else
return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
}
set
{
this.ViewState["CurrentPage"] = value;
}
}
private void BindGrid()
{
string sql = "SELECT [id], [Fname], [Lname], [Username], [Email], [DOB] FROM [Table_Login]";
SqlDataAdapter da = new SqlDataAdapter(sql,ConfigurationManager.ConnectionStrings["mystring"].ConnectionString);
DataTable dt = new DataTable();
da.Fill(dt);
pds.DataSource = dt.DefaultView;
pds.AllowPaging = true;
pds.PageSize = 3;
pds.CurrentPageIndex = CurrentPage;
LinkButton3.Enabled = !pds.IsLastPage;
LinkButton2.Enabled = !pds.IsFirstPage;
DataList1.DataSource = pds;
DataList1.DataBind();
doPaging();
}
private void doPaging()
{
DataTable dt = new DataTable();
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");
for (int i = 0; i < pds.PageCount; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i + 1;
dt.Rows.Add(dr);
}
DataList2.DataSource = dt;
DataList2.DataBind();
}
protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("lnkbtnPaging"))
{
CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
BindGrid();
}
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
BindGrid();
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
CurrentPage += 1;
BindGrid();
}
//protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
//{
// CurrentPage = 0;
// BindGrid();
//}
protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
{
LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("LinkButton1");
if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
{
lnkbtnPage.Enabled = false;
lnkbtnPage.Font.Bold = true;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListPagination.aspx.cs" Inherits="DataListPagination" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="Default.aspx">Default</asp:HyperLink>
<br /><br /><br /><br />
<div>
<asp:DataList ID="DataList1" runat="server" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
DataKeyField="id" GridLines="Both"
RepeatColumns="3" RepeatDirection="Horizontal">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<ItemStyle BackColor="White" ForeColor="#003399" />
<ItemTemplate>
id:
<asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />
<br />
Fname:
<asp:Label ID="FnameLabel" runat="server" Text='<%# Eval("Fname") %>' />
<br />
Lname:
<asp:Label ID="LnameLabel" runat="server" Text='<%# Eval("Lname") %>' />
<br />
Username:
<asp:Label ID="UsernameLabel" runat="server" Text='<%# Eval("Username") %>' />
<br />
Email:
<asp:Label ID="EmailLabel" runat="server" Text='<%# Eval("Email") %>' />
<br />
DOB:
<asp:Label ID="DOBLabel" runat="server" Text='<%# Eval("DOB") %>' />
<br />
<br />
</ItemTemplate>
<SelectedItemStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
</asp:DataList>
<asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">Previous</asp:LinkButton>
<asp:DataList ID="DataList2" runat="server"
onitemcommand="DataList2_ItemCommand"
onitemdatabound="DataList2_ItemDataBound" RepeatColumns="10"
RepeatDirection="Horizontal">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("PageIndex") %>' CommandName="lnkbtnPaging" Text='<%# Eval("PageText") %>'></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
<asp:LinkButton ID="LinkButton3" runat="server" onclick="LinkButton3_Click">Next</asp:LinkButton>
<br />
</div>
</form>
</body>
DataListPagination.aspx.cs ::
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class DataListPagination : System.Web.UI.Page
{
PagedDataSource pds = new PagedDataSource();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
public int CurrentPage
{
get
{
if (this.ViewState["CurrentPage"] == null)
return 0;
else
return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
}
set
{
this.ViewState["CurrentPage"] = value;
}
}
private void BindGrid()
{
string sql = "SELECT [id], [Fname], [Lname], [Username], [Email], [DOB] FROM [Table_Login]";
SqlDataAdapter da = new SqlDataAdapter(sql,ConfigurationManager.ConnectionStrings["mystring"].ConnectionString);
DataTable dt = new DataTable();
da.Fill(dt);
pds.DataSource = dt.DefaultView;
pds.AllowPaging = true;
pds.PageSize = 3;
pds.CurrentPageIndex = CurrentPage;
LinkButton3.Enabled = !pds.IsLastPage;
LinkButton2.Enabled = !pds.IsFirstPage;
DataList1.DataSource = pds;
DataList1.DataBind();
doPaging();
}
private void doPaging()
{
DataTable dt = new DataTable();
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");
for (int i = 0; i < pds.PageCount; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i + 1;
dt.Rows.Add(dr);
}
DataList2.DataSource = dt;
DataList2.DataBind();
}
protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("lnkbtnPaging"))
{
CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
BindGrid();
}
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
BindGrid();
}
protected void LinkButton3_Click(object sender, EventArgs e)
{
CurrentPage += 1;
BindGrid();
}
//protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
//{
// CurrentPage = 0;
// BindGrid();
//}
protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
{
LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("LinkButton1");
if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
{
lnkbtnPage.Enabled = false;
lnkbtnPage.Font.Bold = true;
}
}
}
No comments:
Post a Comment