private static string MyConnString = ConfigurationManager.AppSettings["SqlConnectionString"].ToString();
SqlConnection MyConn;
int PageSize, RecordCount, PageCount, CurrentPage;
protected void Page_Load(object sender, EventArgs e)
{
//设定PageSize
PageSize = 6;
MyConn = new SqlConnection(MyConnString);
MyConn.Open();
//第一次请求执行
if (!Page.IsPostBack)
{
CurrentPage = 0;
ViewState["PageIndex"] = 0;
//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text = RecordCount.ToString();
//计算总共有多少页
if (RecordCount < PageSize)
PageCount = 1;
if (RecordCount % PageSize == 0)
PageCount = RecordCount / PageSize;
else
PageCount = RecordCount / PageSize + 1;
lblPageCount.Text = PageCount.ToString();
ViewState["PageCount"] = PageCount;
ListBind();
}
}
//计算总共有多少条记录
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from album where album_UserId=" + Convert.ToInt32(Session["user_Id"]);
SqlCommand MyComm = new SqlCommand(strCount, MyConn);
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 album where album_UserId=" + Convert.ToInt32(Session["user_Id"]);
DataSet ds = new DataSet();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, MyConn);
MyAdapter.Fill(ds, StartIndex, PageSize, "album");
return ds.Tables["album"].DefaultView;
}
public void ListBind()
{
this.ibtnPrev.ImageUrl = "~/userImg/toolbarMovePrev.gif";
this.ibtnNext.ImageUrl = "~/userImg/toolbarMoveNext.gif";
this.DataList1.DataSource = CreateSource();
this.DataList1.DataBind();
this.ibtnNext.Enabled = true;
this.ibtnPrev.Enabled = true;
if (CurrentPage == PageCount-1)
{
this.ibtnNext.ImageUrl = "~/userImg/toolbarMoveNextDisabled.gif";
this.ibtnNext.Enabled = false;
}
if (CurrentPage == 0)
{
this.ibtnPrev.ImageUrl = "~/userImg/toolbarMovePrevDisabled.gif";
this.ibtnPrev.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 (CurrentPage < PageCount-1)
CurrentPage++;
break;
case "prev":
if (CurrentPage > 0)
CurrentPage--;
break;
}
ViewState["PageIndex"] = CurrentPage;
ListBind();
}html页面按钮代码中要加入:
<asp:ImageButton ID="ibtnNext" runat="server" CommandName="next" ImageUrl="~/userImg/toolbarMoveNext.gif"
OnCommand="Page_OnClick" />

浙公网安备 33010602011771号