码迷,mamicode.com
首页 > Web开发 > 详细

.net中实现aspnetpager分页

时间:2015-06-12 16:29:09      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

第一步首先导入aspnetpager控件,然后再把他从工具箱中拖出,代码如下:

 <webdiyer:AspNetPager ID="aspnetpager1" runat="server" CustomInfoTextAlign="Left"
            FirstPageText="首页 " LastPageText ="末页" NextPageText=" 下一页" NumericButtonCount ="5"
            PageIndexBoxType="TextBox" PrevPageText="上一页 " ShowPageIndexBox ="Never" PageSize="20"
            AlwaysShow="True" CssClass="paginator" CurrentPageButtonClass="cpb" Wrap="True"
            BorderStyle="None" Width="100%" UrlPaging="True" CustomInfoClass="info" CustomInfoHTML =""
            CustomInfoSectionWidth="0px" OnPageChanged="aspnetpager1_PageChanged">
        </webdiyer: AspNetPager>
并且在页面的上部要引用下面代码:
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
之后要在引用里添加aspnetpager.dll引用
 
然后在后台page_load里添加以下分页代码:
                    Page_gkxx sp = new Page_gkxx();
 
                    aspnetpager1.EnableUrlRewriting = true ;
                aspnetpager1.UrlRewritePattern = "/zuowen/list-" + ParentTypeId + "-" + TypeId + "-" + GradeId + "-{0}.html" ;
                sp.InitBindData(Repeater1, aspnetpager1, "zuowen" , "id" , Sear(), GetFields());
其中InitBindData方法代码为:
public void InitBindData( Control c, Wuqi.Webdiyer. AspNetPager aspnet, string tbname, string tbcolumn, stringsqlwhere,string sfields)
        {
            aspnet.RecordCount = GetRows(tbname, sqlwhere);
            if (aspnet.RecordCount > 0)
            {
                string contype = c.GetType().ToString();
 
                DataSet ds = GetList(tbname, tbcolumn,sfields,aspnet.PageSize, aspnet.CurrentPageIndex, sqlwhere);
 
 
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (contype.IndexOf("GridView" ) != -1)
                    {
                        GridView gv = (GridView )c;
                        gv.DataSource = ds;
                        gv.DataBind();
                    }
                    else if (contype.IndexOf( "Repeater") != -1)
                    {
                        Repeater rep = (Repeater )c;
                        rep.DataSource = ds;
                        rep.DataBind();
                    }
                    else if (contype.IndexOf( "DataList") != -1)
                    {
                        DataList dl = (DataList )c;
                        dl.DataSource = ds;
                        dl.DataBind();
                    }
                    c.Visible = true ;
                }
            }
            else
            {
                c.Visible = false ;
            }
 
        }
private int GetRows( string tablename, string strwhere)
        {
            string strSql = "select count(1) from " + tablename + " with(nolock) where " + strwhere + "" ;
            return int .Parse( DbHelperSQL.GetSingle(strSql).ToString());
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetList( string tablename, string keyname, string sfields, int  PageSize, int PageIndex, stringstrWhere)
        {
            SqlParameter [] parameters = {
                                                                                 new SqlParameter ( "@tblName"SqlDbType .VarChar, 255),
                                                                                 new SqlParameter ( "@fldName"SqlDbType .VarChar, 255),
                    new SqlParameter ( "@sFields", SqlDbType .VarChar,1000),
                                                                                 new SqlParameter ( "@PageSize"SqlDbType .Int),
                                                                                 new SqlParameter ( "@PageIndex"SqlDbType .Int),
                                                                                 new SqlParameter ( "@IsReCount"SqlDbType .Bit),
                                                                                 new SqlParameter ( "@OrderType"SqlDbType .Bit),
                                                                                 new SqlParameter ( "@strWhere"SqlDbType .VarChar,1000),
                                                                                };
            parameters[0].Value = tablename;
            parameters[1].Value = keyname;
            parameters[2].Value = sfields;
            parameters[3].Value = PageSize;
            parameters[4].Value = PageIndex;
            parameters[5].Value = 0;
            parameters[6].Value = 1;
            parameters[7].Value = strWhere;
            return DbHelperSQL .RunProcedure( "Up_GetRecordByPage", parameters, "ds" );
        }
这是在DbHelperSQL 中的方法:
  /// <summary>
    /// 执行存储过程返回dataset
    /// </summary>
    /// <param name="storedProcName"> 存储过程名 </param>
    /// <param name="parameters"> 存储过程参数 </param>
    /// <param name="tableName"> DataSet结果中的表名 </param>
    /// <returns> DataSet</returns>
    public static DataSet RunProcedure( string storedProcName, IDataParameter [] parameters, string tableName)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            DataSet dataSet = new DataSet();
            connection.Open();
            SqlDataAdapter sqlDA = new SqlDataAdapter();
            sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
            sqlDA.Fill(dataSet, tableName);
            connection.Close();
            return dataSet;
        }
    }
 
    /// <summary>
    /// 执行一条计算查询结果语句,返回查询结果(object)。
    /// </summary>
    /// <param name="SQLString"> 计算查询结果语句 </param>
    /// <returns> 查询结果(object) </returns>
    public static object GetSingle( string SQLString)
    {
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            using (SqlCommand cmd = new SqlCommand(SQLString, connection))
            {
                try
                {
                    connection.Open();
                    object obj = cmd.ExecuteScalar();
                    if ((Object .Equals(obj, null)) || ( Object .Equals(obj, System.DBNull .Value)))
                    {
                        return null ;
                    }
                    else
                    {
                        return obj;
                    }
                }
                catch (System.Data.SqlClient.SqlException e)
                {
                    connection.Close();
                    connection.Dispose();
 
                    throw new Exception(e.Message);
                }
 
            }
        }
    }
这是Sear()方法的代码:
         /// <summary>
        /// 根据传值进行搜索分页
        /// </summary>
        /// <returns></returns>
        private string Sear()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append( "1=1" );
            if (ParentTypeId != 0)
                sb.AppendFormat( " and ptypeid={0}" ,ParentTypeId);
            if (TypeId != 0)
                sb.AppendFormat( " and typeid={0}" ,TypeId);
            if (GradeId != 0)
                sb.AppendFormat( " and gid={0}" , GradeId);
            return sb.ToString();
        }
这是获取分页中显示的内容代码:
  private string GetFields()
        {
            return "id,title,addtime,Content,typeid,ptypeid,views" ;
        }
 
接着添加分页的事件:
private void ZuowenHotBind()
        {
            DataSet ds = ZuowenDal .GetList(10, "addtime>‘" + DateTime .Now.AddMonths(-3).ToString() + "‘" "views desc" );
            if (ds.Tables[0].Rows.Count > 0)
            {
                RepHot.DataSource = ds;
                RepHot.DataBind();
            }
        }
以上操作基本完成了aspnetpager分页是实现,又不懂的问题可以直接评论回复。
(代码用到了封装的DbHelperSQL.cs)

.net中实现aspnetpager分页

标签:

原文地址:http://www.cnblogs.com/liuyanpeng/p/4571658.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!