與DataSource用法差不多
由於DataList本身沒有帶分頁功能,pageDateSource禰補了這個不足,妳可以當作DateSource來使用,沒有太大區別
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;
using System.Text.RegularExpressions;
public partial class _Default : System.Web.UI.Page
{
SqlHelper MySqlHelper = new SqlHelper(); // 我寫的數據庫操作方法集,創建壹個實例
Operation MyOperation = new Operation(); // 我寫的網站通用處理方法集,創建壹個實例
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GbookData();
lb_count.Text = MySqlHelper.MyScalar("SELECT COUNT(*) FROM BBS WHERE BBS_IsDisplay = '1'"); // 記錄條數
}
}
protected void GbookData() // 分頁程序代碼
{
string strURL = Request.RawUrl;
int intPage = 1;
PagedDataSource MyPager = new PagedDataSource();
MyPager.DataSource = MySqlHelper.MyDS("SELECT * FROM BBS WHERE BBS_IsDisplay = '1' ORDER BY BBS_ID DESC").Tables["MyTable"].DefaultView;
MyPager.PageSize = 9;
MyPager.AllowPaging = true;
hpl_first.Enabled = true;
hpl_up.Enabled = true;
hpl_down.Enabled = true;
hpl_end.Enabled = true;
if (strURL.Contains("/Page/"))
{
string strPage = strURL.Substring(strURL.LastIndexOf("/Page/") + 6);
if (strURL.Substring(strURL.LastIndexOf("/")) == "/")
{
Response.Redirect("NoAccess.htm");
}
if (Regex.IsMatch(strPage, "\\d"))
{
int thisPage;
try
{
thisPage = Convert.ToInt32(strURL.Substring(strURL.LastIndexOf("/Page/") + 6));
if (thisPage < 1 || thisPage > MyPager.PageCount)
{
Response.Redirect("NoAccess.htm");
}
else
{
intPage = thisPage;
}
}
catch
{
Response.Redirect("NoAccess.htm");
}
}
else
{
Response.Redirect("NoAccess.htm");
}
}
hpl_first.NavigateUrl = "~/GuestBook/Page/1";
hpl_up.NavigateUrl = "~/GuestBook/Page/" + (intPage - 1).ToString();
hpl_down.NavigateUrl = "~/GuestBook/Page/" + (intPage + 1).ToString();
hpl_end.NavigateUrl = "~/GuestBook/Page/" + MyPager.PageCount;
MyPager.CurrentPageIndex = intPage - 1;
Repeater_1.DataSource = MyPager;
Repeater_1.DataBind();
if (MyPager.IsFirstPage)
{
hpl_first.Enabled = false;
hpl_up.Enabled = false;
}
if (MyPager.IsLastPage)
{
hpl_down.Enabled = false;
hpl_end.Enabled = false;
}
lb_thispage.Text = intPage.ToString(); // 當前頁碼
lb_totalpage.Text = MyPager.PageCount.ToString(); // 總頁數
}
}