關於asp.net分頁

private void list_bind()
    {
        
        SqlConnection conn = Class1.DBconnection();
        if (Request.QueryString["Class_ID"] != "")
        {
            id = Request.QueryString["Class_ID"];
            sql = "select n_ID,n_Big_ID,n_Title,n_Source,n_Content,n_Time from News where n_Big_ID=" + id + " order by n_ID desc";
        }
        else
        {
            sql = "select n_ID,n_Big_ID,n_Title,n_Source,n_Content,n_Time from News order by n_ID desc";
        }


        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataTable dt = ds.Tables[0];


        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (ds.Tables[0].Rows[i]["n_Title"].ToString().Length > 100)
            {
                ds.Tables[0].Rows[i]["n_Title"] = ds.Tables[0].Rows[i]["n_Title"].ToString().Substring(0, 100) + "...";
            }
        }


        if (dt.Rows.Count > 0)
        {
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = ds.Tables[0].DefaultView;


            pds.AllowPaging = true;
            pds.PageSize =17;
            int curPage;


            if (Request.QueryString["page"] != null)
            {
                curPage = Convert.ToInt32(Request.QueryString["page"]);
            }
            else
            {
                curPage = 1;
            }


            pds.CurrentPageIndex = curPage - 1;
            this.Repeater1.DataSource = pds;
            this.Repeater1.DataBind();
            this.Label1.Text += "<a>共有<font color=red><b>" + ds.Tables[0].Rows.Count.ToString() + "</b></font>條記錄</a>&nbsp;";
            this.Label1.Text += "<a>每頁顯示<font color=red><b>" + pds.PageSize + "</b></font>條</a>&nbsp;";
            this.Label1.Text += "<a>共<font color=red><b>" + pds.PageCount.ToString() + "</b></font>頁</a>&nbsp;";
            this.Label1.Text += "<a>當前第<font color=red><b>" + curPage.ToString() + "</b></font>頁</a>&nbsp;&nbsp;";
            if (curPage == 1)
            {
                this.Label1.Text += "<a>首頁</a>&nbsp;";
                this.Label1.Text += "<a>上一頁</a>&nbsp;";
            }
            else
            {
                this.Label1.Text += "<a href='News_Manage.aspx?page=1&Class_ID=" + id + "'>首頁</a>&nbsp;";
                this.Label1.Text += "<a href='News_Manage.aspx?page=" + (curPage - 1).ToString() + "&Class_ID=" + id + "'>上一頁</a>&nbsp;&nbsp;";
            }
            if (curPage == pds.PageCount)
            {
                this.Label1.Text += "<a>下一頁</a>&nbsp;";
                this.Label1.Text += "<a>尾頁</a>&nbsp;";
            }
            else
            {
                this.Label1.Text += "<a href='News_Manage.aspx?page=" + (curPage + 1).ToString() + "&Class_ID=" + id + "'>下一頁</a>&nbsp;";
                this.Label1.Text += "<a href='News_Manage.aspx?page=" + pds.PageCount.ToString() + "&Class_ID=" + id + "'>尾頁</a>&nbsp;";
            }


            string HtmlSelectString = "<select οnchange=\"javascript:window.location=this.value\">";
            for (int i = 1; i <= pds.PageCount; i++)
            {
                if (i == curPage)
                {
                    HtmlSelectString += "<option value=?page=" + i + "&Class_ID=" + id + " selected>第" + i + "頁</option>";
                }
                else
                {
                    HtmlSelectString += "<option value=?page=" + i + "&Class_ID=" + id + ">第" + i + "頁</option>";
                }
            }
            HtmlSelectString += "</select>";


            this.Label1.Text += "<a>" + HtmlSelectString + "</a>";
        }
        else
        {
            this.Label1.Text = "當前沒有記錄";
        }
        ds.Dispose();
        da.Dispose();

        conn.Close();

}

效果如下:

   
發佈了19 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章