DataList 能否分页,请问如何实现?
2023-07-06
private void Page_Load(object sender, System.EventArgs e)
PageSize=1;
Conn= new SqlConnection(Application["Guest_Conn"].ToString());
Conn.Open();
if(!Page.IsPostBack)
ListBind();
CurrentPage = 0;
ViewState["PageIndex"] = 0;
//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
//计算总共有多少页
PageCount = RecordCount/PageSize;
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
//计算总共有多少条记录
public int CalculateRecord()
int intCount;
string strCount = “select count(*) as co from guest”;
SqlCommand MyComm = new SqlCommand (strCount,Conn);
SqlDataReader dr = MyComm.ExecuteReader();
if(dr.Read())
intCount = Int32.Parse(dr["co"].ToString());
else
intCount = 0;
dr.Close();
return intCount;
ICollection CreateSource()
int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = “Select * from guest order by postdate desc”;
DataSet ds = new DataSet();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,Conn);
MyAdapter.Fill(ds,StartIndex,PageSize,”guest”);
return ds.Tables["guest"].DefaultView;
public void ListBind()
Myguest.DataSource = CreateSource();
Myguest.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage+1).ToString();
public void Page_OnClick(Object sender,CommandEventArgs e)
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch(cmd)
case “next”:
if(CurrentPage0) CurrentPage–;
break;
ViewState["PageIndex"] = CurrentPage;
ListBind();