程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> datalist結合PagedDataSource 類進行分頁

datalist結合PagedDataSource 類進行分頁

編輯:.NET實例教程

 public void sel_callname()
    {

          PagedDataSource ps = new PagedDataSource();//用來給數據控件顯示分頁的類不能繼承
        int curpage = Convert.ToInt32(this.lblCurrentIndex.Text.Trim());
        //獲取數據源
        string sqlstr = "select articleid,title,classname,username,dateandtime From db_article";
        com = new SqlCommand(sqlstr, con);
        com.CommandType = CommandType.Text;
        da = new SqlDataAdapter();
        da.SelectCommand = com;
        ds = new DataSet();
        da.Fill(ds);
        ps.DataSource = ds.Tables[0].DefaultVIEw;
        ps.AllowPaging = true;//是否可以分業
        ps.PageSize = 3;//每頁顯示數據的數量
        ps.CurrentPageIndex = curpage - 1;//取得當前的頁碼
        this.LinkButton2.Enabled = true;
        this.Linkbutton3.Enabled = true;
        this.Linkbutton4.Enabled = true;
        this.lblCurrentIndex.Enabled = true;
        //如果是當前第一頁
        if (curpage == 1)
        {
            this.btnFirst.Enabled = false;//不顯示第一頁按鈕
            this.LinkButton2.Enabled = false;//不顯示上一頁按鈕
        }
        //如果是最後一頁
        if (curpage == ps.PageCount)
        {
      &nb

$False$

sp;     this.Linkbutton3.Enabled = false;//不顯示下一頁按鈕
            this.Linkbutton4.Enabled = false;//不顯示最後一頁按鈕
        }
        this.lblPageCount.Text = Convert.ToString(ps.PageCount);//得到最後一頁
        for (int i = 1; i <= ps.PageCount; i++)//填充下拉列表值
        {
            DropDownList2.Items.Add("" + i);
        }
        try
        {
            GridVIEw1.DataSource = ps;
            GridVIEw1.DataBind();
        }
        catch (SqlException ex)
        {
            myLabel.Text = "數據庫錯誤!" + ex.Message;
        }
        finally
        {
            ds.Clone();
            com.Clone();
            con.Close();
        }
    }
//怎樣獲得給DataList中添加一個Bootn按鈕進行刪除的功能
//首先綁定datalist,str是傳進來的任何有效的sql查詢語句
    protected void Data_x(string str)
    {
        con = new SqlConnection(ConfigurationSettings.APPSettings["dsn"]);
        da = new SqlDataAdapter(str, con);
        DataSet ds = new DataSet();
        try
        {
&nbs

p;           da.Fill(ds, "table");
            DataList1.DataSource = ds;
            DataList1.DataKeyFIEld = "xid";//用來綁定主鍵這點很重要
            DataList1.DataBind();
        }
        catch (SqlException ex)
        {
            throw (ex);
        }
        finally
        {
            ds.Clone();
            con.Close();
        }
  &n
bsp; }
//下面是通過DataList1的ItemCommand事件來獲得添加到DataList中的按鈕事件
 protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        con = new SqlConnection(System.Configuration.ConfigurationManager.APPSettings["dsn"]);
        //DataList1.DataKeys[e.Item.ItemIndex].ToString();用來獲得DataList1.DataKeyFIEld = "xid";//用來綁定主鍵
        string del_str = "delete from bbs_xinxi where xid=" + DataList1.DataKeys[e.Item.ItemIndex].ToString();
        com = new SqlCommand(del_str, con);
        try
        {
            con.Open();
            if (com.ExecuteNonQuery() > 0)
            {//下面是刪除成功後從新綁定DataList
                string _str = "select xid,xuname,xdate,

xtext from bbs_xinxi where xjname=''" + u_name +
            "''and xyidu=1 order by xdate desc";
                Data_x(_str);
            }
            else
            {
                Page.RegisterStartupScript("key","<script>此信息不能被刪除!</script>");
            }
            
        }
        catch (SqlException ex)
        {
            throw (ex);
        }
        finally
        {
            con.Close();
        }
     } 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved