zoukankan      html  css  js  c++  java
  • 分页类,datalist分页;(C#)asp.net

    using System.Data;
    using System.Configuration;
    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;

    /// <summary>
    /// Showpage 的摘要说明
    /// </summary>
        public class ShowPage
        {
            public ShowPage(int page_size, SqlDataAdapter ds)
            {
                ds.Fill(this.bataset);
                count = this.bataset.Tables[0].Rows.Count;
                this.total_page = count / page_size + 1;
                this.ds = ds;
                this.page_size = page_size;
                //
                // TODO: 在此处添加构造函数逻辑
                //
            }
            public ShowPage()
            {
            }
            int page_size;//每页显示记录条数

            public int Page_size
            {
                get { return page_size; }
                set { page_size = value; }
            }

            SqlDataAdapter ds = new SqlDataAdapter();
            int page_index;
            public int Page_index
            {
                get { return page_index; }
                set { page_index = value; }
            }
            int count;//总记录条数

            public int Count
            {
                get { return count; }
                set { count = value; }
            }
            int total_page;//总页数

            public int Total_page
            {
                get { return total_page; }
                set { total_page = value; }
            }
            DataSet bataset = new DataSet();//数据库记录集合
            public DataSet Bataset
            {
                get { return bataset; }
                set { bataset = value; }
            }
            DataSet aataset = new DataSet();//每页记录集合,用于显示分页
            public DataSet Aataset
            {
                get { return aataset; }
                set { aataset = value; }
            }
            public void ShowPageset()
            {
                int page_index;
                page_index = this.page_index;
                if (page_index * page_size <= count)
                {

                    ds.Fill(aataset, page_size * (page_index - 1), page_size, "temp");
                }
                else
                {
                    ds.Fill(aataset, page_size * (page_index - 1), count - page_size * (page_index - 1), "temp");
                }
            }
      }

    ----------------分页类完毕------------------------------------------------------------------------------------------------------------------------

    看来这里写东西还有讲究,上边那么多东西不知道怎么用;

    先来看下示范吧,对于新手来说他实在是太好用了!

    可以给控件当数据源!这里面的newpage 是一个datalist控件。这样就可以给datalist 控件分页了。是不是很简单

     protected void Page_Load(object sender, EventArgs e)
        {
            //接收传入参数。判断查询内容。循环显示内容。
            SqlConnection conn = new SqlConnection("server=.;database=OilWeb;uid=sa;pwd=qwe2323478");
            SqlDataAdapter adapter = new SqlDataAdapter("select imageurl,oilname,oilprice,decration  from Oil_product", conn);
            ShowPage newpage = new ShowPage(9, adapter);
            total_count = newpage.Count;
          
            if (IsPostBack)
            {
                newpage.Page_index = Int32.Parse(Request.QueryString["page_index"]);
            }
            else
            {
                newpage.Page_index = 1;
            }
            newpage.ShowPageset();
            leibiao.DataSource = newpage.Aataset.Tables["temp"];
            leibiao.DataBind();

    }

    在来看一个一个演示吧。这里动态加载用户控件。实现分页。有分页导航。不好过导航就很粗糙了。希望大家给出更好的代码

     protected void Page_Load(object sender, EventArgs e)
        {
            string type;
            string type_url;
            if (Request.QueryString["type"] == "" || Request.QueryString["type"]==null || Request.QueryString["type"]==string.Empty)
            {
                type = null;
                type_url=null;
            }
            else
            {
                type = "where Oiltype=" + "'" + Request.QueryString["type"] + "'" + "";
                type_url = type + Request["type"].ToString();
             
            } 
          
               int index_str;
                if(Request["index"] ==null )
                {
                    Session["page_index"] = 1; 
                }
            else
            {
                index_str =int.Parse( Request["index"]);
               Session["page_index"] = index_str;
            }
            ShowPage aa = new ShowPage(10, new SqlDataAdapter("SELECT * FROM Oil_product"+" "+type+"" , (new SqlConnection("server=.;database=aspnetdb;uid=sa;pwd=qwe2323478"))));
            aa.Page_index = int.Parse(Session["page_index"].ToString());
            aa.ShowPageset();
            Session["Total_page"] = aa.Total_page;//要定义一个Session["Total_page"]用作全局变量,全局变量的方法有很多,。显然我这不是最好的
            if (!IsPostBack)
            {
                for (int i = 1; i <= aa.Total_page; i++)
                {
                    string str = "<a href=\"Default2.aspx?" + type_url + "&index=" + i + "\">" + i.ToString() + "</a>";
                    suoyin.InnerHtml += str;
                }
            }
                for (int i = 1; i <= aa.Aataset.Tables[0].Rows.Count; i++)
                {

                     //modules_WebUserControl2就是用户控件。具体的属性一目了然了
                    Control c = LoadControl("modules/WebUserControl2.ascx");
                    ((modules_WebUserControl2)c).Oilname = aa.Aataset.Tables[0].Rows[i - 1]["Oilname"].ToString();
                    ((modules_WebUserControl2)c).Oilstyle = aa.Aataset.Tables[0].Rows[i - 1]["Oiltype"].ToString();
                    ((modules_WebUserControl2)c).Oilprice = decimal.Parse(aa.Aataset.Tables[0].Rows[i - 1]["Oilprice"].ToString());
                    ((modules_WebUserControl2)c).Imgurl = aa.Aataset.Tables[0].Rows[i - 1]["OilimageUrl"].ToString();
                    ((modules_WebUserControl2)c).Oilid = int.Parse(aa.Aataset.Tables[0].Rows[i - 1]["OilId"].ToString());
                    Page.Controls[3].Controls.Add(c);
                }
        }

    大卖都很简单!第一次写东西,还请大家多多包含,高手们多多指点。有不懂的地方可以给我留言。

  • 相关阅读:
    BZOJ1511: [POI2006]OKR-Periods of Words
    BZOJ1009: [HNOI2008]GT考试
    BZOJ1355: [Baltic2009]Radio Transmission
    BZOJ1415: [Noi2005]聪聪和可可
    BZOJ1004: [HNOI2008]Cards
    UVA11077 Find the Permutations
    LA3641 Leonardo's Notebook
    UVA10294 Arif in Dhaka
    UVA11762 Race to 1
    UVA11427 Expect the Expected
  • 原文地址:https://www.cnblogs.com/cestbon/p/1653175.html
Copyright © 2011-2022 走看看