AspNetPager與DataList結合實現分頁

iDotNetSpace發表於2008-09-09

AspNetPager與DataList結合實現分頁
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 Default2 : System.Web.UI.Page
{
      protected void Page_Load(object sender, EventArgs e)
      {
          Bind();
      }
      private void Bind()
      {
        //將總條數放到分頁控制元件中
          DataTable dt1 = null;
          dt1 = DataCtrl.selecttable("select count(classname) from mess_class","tabnl");
          // this.pager.RecordCount=System.Convert.ToInt32(ds.Tables[0].Rows[0][0]);
          this.AspNetPager1.RecordCount = System.Convert.ToInt32(dt1.Rows[0][0]);
          Bind1();
       
      }
      private void Bind1()
      {
          SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=xuedao;database=zhaoxuedaonet");
          SqlCommand comm = new SqlCommand("select classname from mess_class",conn);
          conn.Open();
          comm.ExecuteNonQuery();

          DataSet ds = new DataSet();
          SqlDataAdapter dapter = new SqlDataAdapter(comm);
          dapter.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex-1),AspNetPager1.PageSize,"table");
          this.DataList1.DataSource = ds.Tables["table"].DefaultView;
          this.DataList1.DataBind();
          //AspNetPager1.CustomInfoText = "記錄總數:" + AspNetPager1.RecordCount.ToString() + "";
          //AspNetPager1.CustomInfoText = "總頁數:" + AspNetPager1.PageCount.ToString() + "";
          //AspNetPager1.CustomInfoText = " 當前頁:" + AspNetPager1.CurrentPageIndex.ToString() + "";

          //myda.Fill(ds, pager.PageSize * (pager.CurrentPageIndex - 1), pager.PageSize, "article");
          //this.dgList.DataSource = ds.Tables["article"];
          //this.dgList.DataBind();
          ////動態設定使用者自定義文字內容
          //pager.CustomInfoText = "記錄總數:" + pager.RecordCount.ToString() + "";
          //pager.CustomInfoText += " 總頁數:" + pager.PageCount.ToString() + "";
          //pager.CustomInfoText += " 當前頁:" + pager.CurrentPageIndex.ToString() + "";


      }
      protected void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
      {
          AspNetPager1.CurrentPageIndex = e.NewPageIndex;
          Bind1();
          //pager.CurrentPageIndex = e.NewPageIndex;
          //BindData();
          //System.Text.StringBuilder sb = new StringBuilder("<!--\n");
          //sb.Append("var el=document.all;");
          //sb.Append(this.dgList.ClientID);
          //sb.Append(".scrollIntoView(true);");
          //sb.Append("          //sb.Append("/");
          //sb.Append("script>");
          //if (!Page.IsStartupScriptRegistered("scrollScript"))
          //      Page.RegisterStartupScript("scrollScript", sb.ToString());

      }
}

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-442635/,如需轉載,請註明出處,否則將追究法律責任。

相關文章