求DATALIST 分页 急!!该代码没实现分页
public class addrlist : System.Web.UI.Page{
int PageSize, RecordCount, PageCount, CurrentPage;
protected System.Web.UI.WebControls.Label lblCurrentPage;
protected System.Web.UI.WebControls.Label lblRecordCount;
protected System.Web.UI.WebControls.Button lbnPrevPage;
protected System.Web.UI.WebControls.Button lbnNextPage;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.DataList dltBoard;
private void Page_Load(object sender, System.EventArgs e)
{ //设定PageSize
//string strSQL,strC12;
//int i,j;
if(Session.Count==0)
//Page.Response .Write ("hello");
Page.Response.Redirect("default.aspx");
else
if(Session["UserID"].ToString()=="")
Page.Response.Redirect("default.aspx");
//strC12=Request["CID"].ToString();
//第一次请求执行
if (!Page.IsPostBack)
{
ListBind();
CurrentPage = 0;
ViewState["PageIndex"] = 0;
//计算总共有多少记录
RecordCount = CalculateRecord();
lblRecordCount.Text=RecordCount.ToString();
//计算总共有多少页
PageSize = 3;
PageCount = RecordCount/PageSize;
lblCurrentPage.Text =PageCount.ToString();
ViewState["PageCount"] = PageCount;
}
public void ListBind()
{
//TODO 这个function主要是为了绑定数据
string strC12=Request["CID"].ToString();
SqlConnection objconn=new SqlConnection(ConfigurationSettings.AppSettings["connstr"]);
objconn.Open();
//DataList1.DataSource = CreateSource();
string strSQL="select * from UserInfo where UserClass LIKE'%"+strC12+"%'";
SqlDataAdapter da=new SqlDataAdapter(strSQL,objconn);
DataSet ds=new DataSet();
da.Fill (ds,"addrlist");
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true;
if (CurrentPage == (PageCount - 1))
lbnNextPage.Enabled = false;
if (CurrentPage == 0)
lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage + 1).ToString();
//DataList1.DataSource =ds.Tables ["addrlist"].DefaultView;
DataList1.DataBind ();
}
//计算总共有多少条记录
public int CalculateRecord()
{
//TODO 这个function主要是得到你要在这个DATALIST中显示的记录的条数
int intCount;
string strC12=Request["CID"].ToString();
SqlConnection objconn=new SqlConnection(ConfigurationSettings.AppSettings["connstr"]);
objconn.Open();
string strCount = "select count(*) as co from UserInfo where UserClass LIKE'%"+strC12+"%'";
SqlCommand objcmd=new SqlCommand(strCount,objconn);
SqlDataReader dr;
dr=objcmd.ExecuteReader();
if (dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
protected void lbnNextPage_Click(object sender, EventArgs e)
{
//TODO 用来向前翻页
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
if (CurrentPage < (PageCount - 1))
CurrentPage++;
ViewState["PageIndex"] = CurrentPage;
ListBind();
}
protected void lbnPrevPage_Click(object sender, EventArgs e)
{
//TODO 用来向后翻页
CurrentPage = (int)ViewState["PageIndex"];
PageCount = (int)ViewState["PageCount"];
if (CurrentPage > 0)
CurrentPage--;
ViewState["PageIndex"] = CurrentPage;
ListBind();
}