关于DateList一行绑定多个数据分页(一)cs文件

.cs页面
protected System.Web.UI.WebControls.DataList DataList2;
  int pageSize,recordCount,pageCount,currentPage;
  protected System.Web.UI.WebControls.Label lbRecordCount;
  protected System.Web.UI.WebControls.Label lbCurrentPage;
  protected System.Web.UI.WebControls.Label lbPageCount;
  protected System.Web.UI.WebControls.LinkButton butPrev;
  protected System.Web.UI.WebControls.LinkButton butNext;
  protected System.Web.UI.WebControls.LinkButton LinkButton2;
  protected System.Web.UI.WebControls.LinkButton LinkButton1;
  protected System.Web.UI.WebControls.DropDownList dlsPageIndex;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   pageSize = 18;//一页数据的总数

   if(!Page.IsPostBack)
   {
    currentPage = 0; // 初始化当前页为第0页
    ViewState["CurrentPage"] = 0;

    recordCount = CalculateRecord(); // 计算共有多少条记录 调用方法
    ViewState["RecordCount"] = recordCount;
    if (recordCount%pageSize==0)// 计算共有多少页
    {
     pageCount = (recordCount / pageSize);
    }
    else
    {
     pageCount = (recordCount / pageSize)+1;
    }
    
    ViewState["PageCount"] = pageCount;
    InitDls();
    BindData();
   }   
  }

  public int CalculateRecord()//计算页面数量的方法
  {
   int Count;
   dbmanage dbs=new dbmanage();
   SqlConnection conn;
   conn=dbs.GetConnection();
   string countStr = "select count(*) as Total from hyuser where fag='已审核' ";

   SqlCommand comm = new SqlCommand(countStr,conn);
   SqlDataReader dr = comm.ExecuteReader();
   if(dr.Read())
   {
    Count = int.Parse(dr["Total"].ToString());
   }
   else
   {
    Count = 0;
   }
   conn.Close();
   dr.Close();
   return Count;
  }

  public void BindData()
  {
   int startPage;
   dbmanage dbs=new dbmanage();
   SqlConnection conn;
   conn=dbs.GetConnection();
   startPage = currentPage * pageSize;
   string selectStr ="select coname,userid,addtime,fag from hyuser where fag='已审核' order by addtime desc";
   DataSet ds = new DataSet();

   SqlDataAdapter da = new SqlDataAdapter(selectStr,conn);
   da.Fill(ds,startPage,pageSize,"Message");
   DataList2.DataSource = ds;
   DataList2.DataMember = "Message";
   DataList2.DataBind();
   lbCurrentPage.Text =  (int.Parse(ViewState["CurrentPage"].ToString())+1).ToString();
   lbPageCount.Text = (pageCount).ToString();
   lbRecordCount.Text = recordCount.ToString();
   dlsPageIndex.SelectedIndex = currentPage;
   conn.Close();
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.butPrev.Click += new System.EventHandler(this.butPrev_Click);
   this.butNext.Click += new System.EventHandler(this.butNext_Click);
   this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
   this.LinkButton2.Click += new System.EventHandler(this.LinkButton2_Click);
   this.dlsPageIndex.SelectedIndexChanged += new System.EventHandler(this.dlsPageIndex_SelectedIndexChanged);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void butNext_Click(object sender, System.EventArgs e)
  {
   currentPage = (int)ViewState["CurrentPage"];
   pageCount = (int)ViewState["PageCount"];
   recordCount = (int)ViewState["RecordCount"];

   if((currentPage<pageCount-1))
    currentPage++;
   ViewState["CurrentPage"] = currentPage;
   
   BindData();
  }

  private void butPrev_Click(object sender, System.EventArgs e)
  {
   currentPage = (int)ViewState["CurrentPage"];
   pageCount = (int)ViewState["PageCount"];
   recordCount = (int)ViewState["RecordCount"];

   if(currentPage>0)
    currentPage--;
   ViewState["CurrentPage"] = currentPage;
   BindData();
  }

 

  public void InitDls()
  {
   for(int i=1;i<=(pageCount);i++)
   {
    dlsPageIndex.Items.Add(new ListItem("第"+i+"页",(i-1).ToString()));
   }
  }
  private void dlsPageIndex_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   currentPage = (int)ViewState["CurrentPage"];
   pageCount = (int)ViewState["PageCount"];
   recordCount = (int)ViewState["RecordCount"];
   currentPage = int.Parse(dlsPageIndex.SelectedItem.Value);
   ViewState["CurrentPage"] = currentPage;
   BindData();
  }
  private void LinkButton1_Click(object sender, System.EventArgs e)
  {
   currentPage = (int)ViewState["CurrentPage"];
   pageCount = (int)ViewState["PageCount"];
   recordCount = (int)ViewState["RecordCount"];


   ViewState["CurrentPage"] =0;
   BindData();
  }

  private void LinkButton2_Click(object sender, System.EventArgs e)
  {
   currentPage = (int)ViewState["CurrentPage"];
   pageCount = (int)ViewState["PageCount"];
   recordCount = (int)ViewState["RecordCount"];
   Page.Response.Write(ViewState["PageCount"]);

   ViewState["CurrentPage"] =pageCount-1;
   
  
   BindData();
  
  }
---------------------------------------------------------------------------------------------------------------------


阅读更多

没有更多推荐了,返回首页