其实我是使用了PlaceHolder动态加入LinkButton,DbDataAdapter.Fill (DataSet, Int32, Int32, String) 的方法来实现,代码如下:
当然从性能上来说这样做并不一定最好,但是方便我认为是最重要的。
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.Data.SqlClient;
public partial class about_morephoto : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewState["pageindex"] = 0;
databind();
int PageCount,PageSize;
PageSize=10;
SqlConnection mycon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["001eduConnectionString"].ConnectionString);
SqlCommand mycom = new SqlCommand("select count(*) from [membertupian]", mycon);
mycon.Open();
double dPageCount = Convert.ToDouble(mycom.ExecuteScalar()) / PageSize;
PageCount = (int)Math.Ceiling(dPageCount);
mycon.Close();
ViewState["PageCount"] = PageCount;
}
LinkButton Button1;
for (int inti = 1; inti <= (int)ViewState["PageCount"]; inti++)
{
Button1 = new LinkButton();
Button1.Width = 10;
Button1.Text = inti.ToString();
PlaceHolder1.Controls.Add(Button1);
Button1.Click += new EventHandler(LinkButton1_Click);
Button1.ForeColor = LinkButton1.ForeColor;
Button1.Font.Underline = LinkButton1.Font.Underline;
}
}
protected void databind()
{
int PageSize,CurrentPage;
PageSize = 10;
SqlConnection mycon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["001eduConnectionString"].ConnectionString);
SqlDataAdapter mycom = new SqlDataAdapter("Select [descript], [id], [fileurl] FROM [membertupian] orDER BY [id] DESC", mycon);
DataSet ds = new DataSet();
CurrentPage = (int)ViewState["pageindex"];
mycon.Open();
mycom.Fill(ds, CurrentPage * PageSize, PageSize, "membertupian");
mycon.Close();
DataList1.DataSource = ds;
DataList1.DataBind();
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if ((int)ViewState["pageindex"] > 0)
{
ViewState["pageindex"] = (int)ViewState["pageindex"] - 1;
databind();
}
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
if ((int)ViewState["pageindex"] < (int)ViewState["PageCount"]-1)
{
ViewState["pageindex"] = (int)ViewState["pageindex"] + 1;
databind();
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = Convert.ToInt32(((LinkButton)sender).Text) - 1;
databind();
((LinkButton)sender).ForeColor = System.Drawing.Color.FromName("OrangeRed");
((LinkButton)sender).Font.Underline = true;
}
}
当然从性能上来说这样做并不一定最好,但是方便我认为是最重要的。
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.Data.SqlClient;
public partial class about_morephoto : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewState["pageindex"] = 0;
databind();
int PageCount,PageSize;
PageSize=10;
SqlConnection mycon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["001eduConnectionString"].ConnectionString);
SqlCommand mycom = new SqlCommand("select count(*) from [membertupian]", mycon);
mycon.Open();
double dPageCount = Convert.ToDouble(mycom.ExecuteScalar()) / PageSize;
PageCount = (int)Math.Ceiling(dPageCount);
mycon.Close();
ViewState["PageCount"] = PageCount;
}
LinkButton Button1;
for (int inti = 1; inti <= (int)ViewState["PageCount"]; inti++)
{
Button1 = new LinkButton();
Button1.Width = 10;
Button1.Text = inti.ToString();
PlaceHolder1.Controls.Add(Button1);
Button1.Click += new EventHandler(LinkButton1_Click);
Button1.ForeColor = LinkButton1.ForeColor;
Button1.Font.Underline = LinkButton1.Font.Underline;
}
}
protected void databind()
{
int PageSize,CurrentPage;
PageSize = 10;
SqlConnection mycon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["001eduConnectionString"].ConnectionString);
SqlDataAdapter mycom = new SqlDataAdapter("Select [descript], [id], [fileurl] FROM [membertupian] orDER BY [id] DESC", mycon);
DataSet ds = new DataSet();
CurrentPage = (int)ViewState["pageindex"];
mycon.Open();
mycom.Fill(ds, CurrentPage * PageSize, PageSize, "membertupian");
mycon.Close();
DataList1.DataSource = ds;
DataList1.DataBind();
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
if ((int)ViewState["pageindex"] > 0)
{
ViewState["pageindex"] = (int)ViewState["pageindex"] - 1;
databind();
}
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
if ((int)ViewState["pageindex"] < (int)ViewState["PageCount"]-1)
{
ViewState["pageindex"] = (int)ViewState["pageindex"] + 1;
databind();
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
ViewState["pageindex"] = Convert.ToInt32(((LinkButton)sender).Text) - 1;
databind();
((LinkButton)sender).ForeColor = System.Drawing.Color.FromName("OrangeRed");
((LinkButton)sender).Font.Underline = true;
}
}