有什么办法使每页要使用aspnetpager控件的页面不用写以下代码啊?怎样做可以直接调用??有经验的朋友请帮忙...protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
           SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection();
            sqlconn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["buyconn"];
            SqlCommand cmd = new SqlCommand("cyphoto",sqlconn );
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@pageindex", SqlDbType.Int).Value = 1;
            cmd.Parameters.Add("@pagesize", SqlDbType.Int).Value = 1;
            cmd.Parameters.Add("@docount", SqlDbType.Bit).Value = true;
            cmd.Parameters.Add("@shid", SqlDbType.Int).Value = Convert.ToInt16(Session["shopid"]);
            sqlconn.Open();
            AspNetPager1.RecordCount = (int)cmd.ExecuteScalar();
            sqlconn.Close();
            BindData();
        }
    }
    void BindData()
    {
        SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection();
        sqlconn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["buyconn"];
        SqlCommand cmd = new SqlCommand("cyphoto", sqlconn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@pageindex", SqlDbType.Int).Value = AspNetPager1.CurrentPageIndex;
        cmd.Parameters.Add("@pagesize", SqlDbType.Int).Value = AspNetPager1.PageSize;
        cmd.Parameters.Add("@docount", SqlDbType.Bit).Value = false;
        cmd.Parameters.Add("@shid", SqlDbType.Int).Value = Convert.ToInt16(Session["shopid"]);
        sqlconn.Open();
        DataList1.DataSource = cmd.ExecuteReader();
        DataList1.DataBind();
        sqlconn.Close();
    }
    protected void ChangePage(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        BindData();
    }

解决方案 »

  1.   

    DataList不能简单的就分页的,你写的方法:
     protected void ChangePage(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            BindData();
        }
    根本调用了也没用.
    你那是在Datagrid中的代码.
    如果你想简单点去分页,可以下载个第三方分页的控件,到时候就可以想Datagrid一样使用了
      

  2.   

    public int GetRecordCount ()
    {
               SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection();
                sqlconn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["buyconn"];
                SqlCommand cmd = new SqlCommand("cyphoto",sqlconn );
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@pageindex", SqlDbType.Int).Value = 1;
                cmd.Parameters.Add("@pagesize", SqlDbType.Int).Value = 1;
                cmd.Parameters.Add("@docount", SqlDbType.Bit).Value = true;
                cmd.Parameters.Add("@shid", SqlDbType.Int).Value = Convert.ToInt16(Session["shopid"]);
                sqlconn.Open();
                int RecordCount = (int)cmd.ExecuteScalar();
                sqlconn.Close();
                return RecordCount;      
     }
      
       public object GetDataSource(int CurrentPageIndex,int PageSize)
        {
            SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection();
            sqlconn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["buyconn"];
            SqlCommand cmd = new SqlCommand("cyphoto", sqlconn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@pageindex", SqlDbType.Int).Value = CurrentPageIndex;
            cmd.Parameters.Add("@pagesize", SqlDbType.Int).Value = PageSize;
            cmd.Parameters.Add("@docount", SqlDbType.Bit).Value = false;
            cmd.Parameters.Add("@shid", SqlDbType.Int).Value = Convert.ToInt16(Session["shopid"]);
            sqlconn.Open();
            object obj= cmd.ExecuteReader();
            sqlconn.Close();
            return obj;
        } 把这两个方法放到一个单独的类中,就可以实现简单的封装.你还可以做的更好些
      

  3.   

    首先建立分页类
    Pageing.csusing System;
    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;
    namespace Components
    {
           
        public class Pageing
        {
            public string tableName = "";// "Job";
            public string keyField = "";//"Id";
            public string fieldsName = "*";
            public string sortName = "";//"DateTime DESC";  
            public int sortType = 0;
            public string strWhere = "";//stuff_out_id like '%LL000%'";
            public int currentPageIndex;
            public int pageSize;
            public int pageCount;
            public int recordCount;            
            public DataSet RunPaging()
            {
                SqlConnection conn = DataProvider.GetSqlConnection();
                SqlCommand cmd = new SqlCommand("myPage", conn);            cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;            cmd.Parameters.Add("@tableName", SqlDbType.NVarChar);    //表  名
                cmd.Parameters["@tableName"].Direction = ParameterDirection.Input;
                cmd.Parameters["@tableName"].Value = tableName;            cmd.Parameters.Add("@KeyField", SqlDbType.NVarChar);  //主鍵
                cmd.Parameters["@KeyField"].Direction = ParameterDirection.Input;
                cmd.Parameters["@KeyField"].Value = keyField;            cmd.Parameters.Add("@sortName", SqlDbType.NVarChar);   //排序字段
                cmd.Parameters["@sortName"].Direction = ParameterDirection.Input;
                cmd.Parameters["@sortName"].Value = sortName;            cmd.Parameters.Add("@pageIndex", SqlDbType.Int);    //當前頁的索引
                cmd.Parameters["@pageIndex"].Direction = ParameterDirection.Input;
                cmd.Parameters["@pageIndex"].Value = currentPageIndex;            cmd.Parameters.Add("@pageSize", SqlDbType.Int);    //每頁的記錄數
                cmd.Parameters["@pageSize"].Direction = ParameterDirection.Input;
                cmd.Parameters["@pageSize"].Value = pageSize;            cmd.Parameters.Add("@recordCount", SqlDbType.Int);    //總記錄數
                cmd.Parameters["@recordCount"].Direction = ParameterDirection.Output;            cmd.Parameters.Add("@pageCount", SqlDbType.Int);    //總頁數
                cmd.Parameters["@pageCount"].Direction = ParameterDirection.Output;            cmd.Parameters.Add("@strWhere", SqlDbType.NVarChar);    //查詢條件
                cmd.Parameters["@strWhere"].Direction = ParameterDirection.Input;
                cmd.Parameters["@strWhere"].Value = strWhere;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                dataAdapter.Fill(ds);
                pageCount = (int)cmd.Parameters["@pageCount"].Value;
                recordCount = (int)cmd.Parameters["@recordCount"].Value;
                conn.Dispose();
                conn.Close();
                return ds;
            }
        }}
    然后调用
    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;
    using Components;
    namespace Senlindg.Big
    {
        public partial class News : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    myPaging();
                }
                JavaScript.Confirm(btnDelete, "確定刪除嗎?");        }
            private void myPaging()//调用分页存取过程
            {
                Pageing myPage = new Pageing();
                myPage.tableName = "News";
                myPage.keyField = "Id";
                myPage.fieldsName = "*";
                myPage.sortName = "DateTime DESC";
                myPage.strWhere = "";
                myPage.currentPageIndex = pager.CurrentPageIndex;
                myPage.pageSize = pager.PageSize;
                DataGrid1.DataSource = myPage.RunPaging();
                DataGrid1.DataBind();
                pager.RecordCount = myPage.recordCount;
                pager.CustomInfoText = " 共<font color='#FF8000'><b>" + myPage.recordCount.ToString() + "</b></font>條記錄/";
                pager.CustomInfoText += " <font color=#FF8000'><b>" + myPage.pageCount.ToString() + "</b></font>頁";
                pager.CustomInfoText += " 當前第<font color=\"red\"><b>" + pager.CurrentPageIndex.ToString() + "</b></font>頁";        }        protected void pager_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
            {
                pager.CurrentPageIndex = e.NewPageIndex;
                myPaging();
            }        protected void btnDelete_Click(object sender, EventArgs e)
            {
                string str = txtIndex.Value.Trim();
                if (str == "")
                {
                    JavaScript.Alert("請選擇記錄!");
                    return;
                }
                string[] strIndex = str.Split('|');
                for (int i = 0; i < strIndex.Length; i++)
                {
                    int index = Convert.ToInt32(strIndex[i]);
                    int id = Convert.ToInt32(DataGrid1.Items[index - 1].Cells[5].Text);
                    DTable.DeleteRecord("News", "Id", id);
                }
                txtIndex.Value = "";
                Response.Redirect("News.aspx");
            }           }
    }
      

  4.   

    public object GetDataSource(int CurrentPageIndex,int PageSize)
        {
            SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection();
            sqlconn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["buyconn"];
            SqlCommand cmd = new SqlCommand("cyphoto", sqlconn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@pageindex", SqlDbType.Int).Value = CurrentPageIndex;
            cmd.Parameters.Add("@pagesize", SqlDbType.Int).Value = PageSize;
            cmd.Parameters.Add("@docount", SqlDbType.Bit).Value = false;
            cmd.Parameters.Add("@shid", SqlDbType.Int).Value = Convert.ToInt16(Session["shopid"]);
            sqlconn.Open();
            object obj= cmd.ExecuteReader();
            sqlconn.Close();
            return obj;
        }
    这里的object是什么??
      

  5.   

    要看你的具体情况,如果所有页面用的分页存储过程都差不多一样的话,你可以把上面的方法封装起来,放在通用类中,用时只需要传递相应的参数即可,不需要每次都写这么长的代码,比如上面的BindData方法中的代码就可以提出来做成一个通用类,类似于这个:public static void BindPagedDataToBaseDataList(string spName,int pageIndex,int pageSize,Int16 shid,BaseDataList dlist){
    SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection();
            sqlconn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["buyconn"];
            SqlCommand cmd = new SqlCommand(spName, sqlconn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@pageindex", SqlDbType.Int).Value = pageIndex;
            cmd.Parameters.Add("@pagesize", SqlDbType.Int).Value = pageSize;
            cmd.Parameters.Add("@docount", SqlDbType.Bit).Value = false;
            cmd.Parameters.Add("@shid", SqlDbType.Int).Value = shid;
            sqlconn.Open();
            dlist.DataSource = cmd.ExecuteReader();
            dlist.DataBind();
            sqlconn.Close();
    }
    然后上面BindData中的代码就可以改为:
    void BindData(){
    CommonClass.BindDataToBaseDataList("cyphoto",AspNetPager1.CurrentPageIndex,AspNetPager1.PageSize,Convert.ToInt16(Session["shopid"]),DataList1);
    }上面的方法中之所以用BaseDataList,是如果你绑定的控件是DataGrid的时候也不需要改任何代码,因为DataGrid和DataList都是从BaseDataList继承的。
      

  6.   


    DataList不能简单的就分页的,你写的方法:
     protected void ChangePage(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            BindData();
        }
    根本调用了也没用.
    你那是在Datagrid中的代码.
    如果你想简单点去分页,可以下载个第三方分页的控件,到时候就可以想Datagrid一样使用了 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    只要调用PagedDataSource类就可以分页了.dataGrid也是封装了这个类.
      

  7.   

    感谢webdiyer(陕北吴旗娃) !~搞好了!~不知这样做有没有问题~
        public int binddatalist(string spName, Int16 shid)
            {
                int back = 0;
                SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection();
                sqlconn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["buyconn"];
                SqlCommand cmd = new SqlCommand(spName, sqlconn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@pageindex", SqlDbType.Int).Value = 1;
                cmd.Parameters.Add("@pagesize", SqlDbType.Int).Value = 1;
                cmd.Parameters.Add("@docount", SqlDbType.Bit).Value = true;
                cmd.Parameters.Add("@shid", SqlDbType.Int).Value = shid;
                sqlconn.Open();
                back = (int)cmd.ExecuteScalar();
                sqlconn.Close();
                return back;        }
            public static void BindPagedDataToBaseDataList(string spName, int pageIndex, int pageSize, Int16 shid, BaseDataList dlist)
            {
                SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection();
                sqlconn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["buyconn"];
                SqlCommand cmd = new SqlCommand(spName, sqlconn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@pageindex", SqlDbType.Int).Value = pageIndex;
                cmd.Parameters.Add("@pagesize", SqlDbType.Int).Value = pageSize;
                cmd.Parameters.Add("@docount", SqlDbType.Bit).Value = false;
                cmd.Parameters.Add("@shid", SqlDbType.Int).Value = shid;
                sqlconn.Open();
                dlist.DataSource = cmd.ExecuteReader();
                dlist.DataBind();
                sqlconn.Close();
            }页面调用:   protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {            AspNetPager1.RecordCount = data.binddatalist("cyphoto", Convert.ToInt16(Session["shopid"]));
                BindData();
         
            }
        }    void BindData()
        {        data.BindPagedDataToBaseDataList("cyphoto", AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, Convert.ToInt16(Session["shopid"]), DataList1);
        }    protected void ChangePage(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            BindData();
        }存储过程:CREATE PROCEDURE [dbo].[cyphoto](@pagesize int,
    @pageindex int,
    @shid int,
    @docount bit)
    as
    set nocount on
    if(@docount=1)
    select count(ph_id) from cy_photo where ph_shid = @shid
    else
    begin
    declare @indextable table(ph_id int identity(1,1),nid int)
    declare @PageLowerBound int
    declare @PageUpperBound int
    set @PageLowerBound=(@pageindex-1)*@pagesize
    set @PageUpperBound=@PageLowerBound+@pagesize
    set rowcount @PageUpperBound
    insert into @indextable(nid) select ph_id from cy_photo where ph_shid = @shid order by ph_order desc
    select O.ph_name,O.ph_address,O.ph_id from cy_photo O,@indextable t where O.ph_shid = @shid and O.ph_id=t.nid
    and t.ph_id>@PageLowerBound and t.ph_id<=@PageUpperBound order by ph_order desc
    end
    set nocount off
    GO不知存储过程有没有更好的写法!~
    结贴了!~