datalist實現分頁

分頁效果如下:分頁

 

後臺代碼:code  C#

一 、申明全局變量:

 public static int startIndex =0;      //起始頁
    public static  int pagesize =8;      //每頁記錄數
    public static int pagecount =1;       //總記錄
    public static int countpage = 0;           //總頁數
    public static int nowpage = 1;            //當前頁
    public static   DataSet DS = new DataSet();       //接收數據
    public static string   connstring="Data Source=.//SQLEXPRESS;AttachDbFilename=|DataDirectory|//payforboos.mdf;Integrated Security=True;User Instance=True";   //連接字符串
    static SqlConnection nwindConn = new SqlConnection(connstring);      
    public static   SqlDataAdapter adapter = new SqlDataAdapter("", nwindConn);
    static SqlCommand selCmd = adapter.SelectCommand;    //爲查詢適配器創建查詢語句

 

 protected void Page_Load(object sender, EventArgs e)
    {
     
        if (!IsPostBack)
        {

           selCmd.CommandText = SqlDataSource1.SelectCommand.ToString();
            getdatatable();
            DataList1.DataSource = DS.Tables["allbooks"].DefaultView;
            DataList1.DataBind();
                DataTable dt = new DataTable();
            adapter.Fill(dt);
            pagecount = dt.Rows.Count;
            ViewState["pagecount"] = pagecount;
            countpage = (int)Math.Ceiling((double) pagecount / pagesize);   //計算頁數    向上取整
        }

}

static public void  getdatatable()             //自定義函數
    {

       nwindConn.Close();

       nwindConn.Open();
        DS.Clear();      //將dataset中的數據清空   否則會形成累計
        selCmd.Parameters.Clear();
        adapter.Fill(DS, startIndex, pagesize, "allbooks");          //從第startindex條數據開始 讀取pagesize條數據     添加到DS中

      nwindConn.Close();
    }

 

protected void Button3_Click(object sender, EventArgs e)        //下一頁
    {
        if (nowpage <countpage  )
        {
            startIndex = startIndex + pagesize;
            nowpage++;
        }
        
          selCmd.CommandText = SqlDataSource1.SelectCommand.ToString();
           getdatatable();
           DataList1.DataSource = DS.Tables["allbooks"];
          DataList1.DataBind();
  }

protected void Button2_Click(object sender, EventArgs e)    //上一頁
    {
        if (nowpage  > 1 )
        {
            startIndex -= pagesize;
            nowpage--;
        }  
        selCmd.CommandText = SqlDataSource1.SelectCommand.ToString();
        getdatatable();
         DataList1.DataSource = DS.Tables["allbooks"];
        DataList1.DataBind();

    }

 protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)           //添加頁碼
    {
          foreach (Control c in e.Item.Controls)            //查詢在footerTemplate                                 
        {
            DropDownList dplist = c as DropDownList;
            if (dplist != null)
            {
               
                for (int i = 1; i <=countpage ; i++)
                {
                    dplist.Items.Add(i.ToString());
                    
                 }
              
             }
        }

 

   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)          //查找第I 頁
    {
        DropDownList dplist = (DropDownList)sender;                   //獲取sender對象中的dropdownlist的值
        int selectpage = int.Parse(dplist.SelectedValue.ToString());
        nowpage = selectpage;
        startIndex = (nowpage - 1) * pagesize;
        getdatatable();
        DataList1.DataSource = DS.Tables["allbooks"];
        DataList1.DataBind();
    }

 

 

前臺代碼:

  <FooterTemplate>
                    <table class="footstyle">
                        <tr>
                            <td>
                                總共<b> &nbsp;<%=countpage %> &nbsp;</b>頁/當前第<b> &nbsp;<%=nowpage  %> &nbsp;</b>頁</td>
                            <td>
                                總計<b> &nbsp;<%=pagecount  %> &nbsp;</b>條記錄</td>
                            <td align="right">
                                <asp:Button ID="Button2" runat="server" οnclick="Button2_Click" Text="上一頁" />
                                <asp:DropDownList ID="DropDownList1" runat="server" Height="19px" Width="58px"
                                    AutoPostBack="True"
                                    onselectedindexchanged="DropDownList1_SelectedIndexChanged"  >
                                 
                                </asp:DropDownList>
                                <asp:Button ID="Button3" runat="server" οnclick="Button3_Click" Text="下一頁" />
                            </td>
                        </tr>
                    </table>
                </FooterTemplate>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章