最近友人詢問DataGrid有內建的分頁功能,可是DataList卻沒有,那該怎麼做呢?
以下且看包仔分解。
最近友人詢問DataGrid有內建的分頁功能,可是DataList卻沒有,那該怎麼做呢?
以下且看包仔分解。
DataListTest.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListTest.aspx.cs" Inherits="DataListTest" %>
<!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">
<div>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="5" RepeatDirection="Horizontal">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# GetImg(Eval("AdPicName","{0}")) %>' />
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:DataList></div>
</form>
</body>
</html>
<!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">
<div>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="5" RepeatDirection="Horizontal">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# GetImg(Eval("AdPicName","{0}")) %>' />
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:DataList></div>
</form>
</body>
</html>
DataListTest.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Drawing;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class DataListTest : System.Web.UI.Page
{
PagedDataSource pds;
int pageCount = 5;//取得顯示資料來源中所有項目所需的頁面總數
int pagecount = 5;
int CurrentPage = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetMediaList();
}
}
//綁入圖片連結
public string GetImg(string filename)
{
return "~/DataListImages/" + filename;
}
public void GetMediaList()
{
SqlConnection cn = new SqlConnection(@"server=localhost;uid=sa;pwd=2u031j4xji4j04njo4;database=testDB");
SqlDataAdapter da = new SqlDataAdapter("select * from AdPic", cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = pagecount;
this.DataList1.ItemDataBound += new DataListItemEventHandler(list_ItemDataBound);
try
{
if (Request.QueryString["page"] != null)
CurrentPage = Convert.ToInt32(Request.QueryString["page"]); // int CurrentPage = 0;
else
CurrentPage = 1;
pds.CurrentPageIndex = CurrentPage - 1; // PagedDataSource.CurentPageIndex: 取得或設定目前介面的索引
}
catch
{
}
this.DataList1.DataSource = pds;
this.DataList1.DataBind();
}
void list_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
if (pds.PageCount > 1) // PagedDataSource.PageCount:取得顯示資料來源中所有項目所需的頁面總數(1網頁頁尾顯示幾張分頁數)
{
string mypath = Request.Path;
int first;
int last;
if ((CurrentPage % pageCount) != 0)
{
first = ((CurrentPage / pageCount) * pageCount) + 1;
if (((CurrentPage / (pageCount) * pageCount) + pageCount) <= pds.PageCount)
{
last = ((CurrentPage / pageCount) * pageCount) + pageCount;
}
else
{
last = pds.PageCount;
}
}
else
{
first = ((CurrentPage / (pageCount + 1)) * pageCount) + 1;
if ((((CurrentPage / (pageCount + 1)) * pageCount) + pageCount) <= pds.PageCount)
{
last = ((CurrentPage / (pageCount + 1)) * pageCount) + pageCount;
}
else
{
last = pds.PageCount;
}
}
e.Item.Controls.Add(new LiteralControl("<table width='100%' height='50px'><tr><td height='50%'></td></tr>" +
"<tr align='center'><td Class='p1' >"));
e.Item.Controls.Add(new LiteralControl(string.Format("目前在{0}頁", CurrentPage)));
e.Item.Controls.Add(new LiteralControl(" "));
if (!pds.IsFirstPage)
{
HyperLink newLink = new HyperLink();
newLink.ID = "firstpage";
newLink.Text = "上一頁";
newLink.NavigateUrl = string.Format("{0}?page={1}", mypath, (CurrentPage - 1).ToString());
e.Item.Controls.Add(newLink);
e.Item.Controls.Add(new LiteralControl(" "));
}
for (int i = first; i <= last; i++)
{
HyperLink newLink = new HyperLink();
newLink.ID = string.Format("page0{0}", i.ToString());
newLink.Text = i.ToString();
newLink.NavigateUrl = string.Format("{0}?page={1}", mypath, i.ToString());
e.Item.Controls.Add(newLink);
e.Item.Controls.Add(new LiteralControl(" "));
}
if (!pds.IsLastPage)
{
HyperLink newLink = new HyperLink();
newLink.ID = "lastpage";
newLink.Text = "下一頁";
newLink.NavigateUrl = string.Format("{0}?page={1}", mypath, (CurrentPage + 1).ToString());
e.Item.Controls.Add(newLink);
e.Item.Controls.Add(new LiteralControl(" "));
}
e.Item.Controls.Add(new LiteralControl(string.Format("共{0}頁", pds.PageCount)));
e.Item.Controls.Add(new LiteralControl("</td></tr></table>"));
((HyperLink)e.Item.FindControl(string.Format("page0{0}", CurrentPage))).ForeColor = Color.Black;
}
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Drawing;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class DataListTest : System.Web.UI.Page
{
PagedDataSource pds;
int pageCount = 5;//取得顯示資料來源中所有項目所需的頁面總數
int pagecount = 5;
int CurrentPage = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetMediaList();
}
}
//綁入圖片連結
public string GetImg(string filename)
{
return "~/DataListImages/" + filename;
}
public void GetMediaList()
{
SqlConnection cn = new SqlConnection(@"server=localhost;uid=sa;pwd=2u031j4xji4j04njo4;database=testDB");
SqlDataAdapter da = new SqlDataAdapter("select * from AdPic", cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = pagecount;
this.DataList1.ItemDataBound += new DataListItemEventHandler(list_ItemDataBound);
try
{
if (Request.QueryString["page"] != null)
CurrentPage = Convert.ToInt32(Request.QueryString["page"]); // int CurrentPage = 0;
else
CurrentPage = 1;
pds.CurrentPageIndex = CurrentPage - 1; // PagedDataSource.CurentPageIndex: 取得或設定目前介面的索引
}
catch
{
}
this.DataList1.DataSource = pds;
this.DataList1.DataBind();
}
void list_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
if (pds.PageCount > 1) // PagedDataSource.PageCount:取得顯示資料來源中所有項目所需的頁面總數(1網頁頁尾顯示幾張分頁數)
{
string mypath = Request.Path;
int first;
int last;
if ((CurrentPage % pageCount) != 0)
{
first = ((CurrentPage / pageCount) * pageCount) + 1;
if (((CurrentPage / (pageCount) * pageCount) + pageCount) <= pds.PageCount)
{
last = ((CurrentPage / pageCount) * pageCount) + pageCount;
}
else
{
last = pds.PageCount;
}
}
else
{
first = ((CurrentPage / (pageCount + 1)) * pageCount) + 1;
if ((((CurrentPage / (pageCount + 1)) * pageCount) + pageCount) <= pds.PageCount)
{
last = ((CurrentPage / (pageCount + 1)) * pageCount) + pageCount;
}
else
{
last = pds.PageCount;
}
}
e.Item.Controls.Add(new LiteralControl("<table width='100%' height='50px'><tr><td height='50%'></td></tr>" +
"<tr align='center'><td Class='p1' >"));
e.Item.Controls.Add(new LiteralControl(string.Format("目前在{0}頁", CurrentPage)));
e.Item.Controls.Add(new LiteralControl(" "));
if (!pds.IsFirstPage)
{
HyperLink newLink = new HyperLink();
newLink.ID = "firstpage";
newLink.Text = "上一頁";
newLink.NavigateUrl = string.Format("{0}?page={1}", mypath, (CurrentPage - 1).ToString());
e.Item.Controls.Add(newLink);
e.Item.Controls.Add(new LiteralControl(" "));
}
for (int i = first; i <= last; i++)
{
HyperLink newLink = new HyperLink();
newLink.ID = string.Format("page0{0}", i.ToString());
newLink.Text = i.ToString();
newLink.NavigateUrl = string.Format("{0}?page={1}", mypath, i.ToString());
e.Item.Controls.Add(newLink);
e.Item.Controls.Add(new LiteralControl(" "));
}
if (!pds.IsLastPage)
{
HyperLink newLink = new HyperLink();
newLink.ID = "lastpage";
newLink.Text = "下一頁";
newLink.NavigateUrl = string.Format("{0}?page={1}", mypath, (CurrentPage + 1).ToString());
e.Item.Controls.Add(newLink);
e.Item.Controls.Add(new LiteralControl(" "));
}
e.Item.Controls.Add(new LiteralControl(string.Format("共{0}頁", pds.PageCount)));
e.Item.Controls.Add(new LiteralControl("</td></tr></table>"));
((HyperLink)e.Item.FindControl(string.Format("page0{0}", CurrentPage))).ForeColor = Color.Black;
}
}
}
}