.net中实现aspnetpager分页
阅读原文时间:2024年10月28日阅读:1

第一步首先导入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">

        

并且在页面的上部要引用下面代码:

<%@ 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());

        }

        /// 

        /// 分页获取数据列表

        /// 

        /// 

        /// 

        /// 

        /// 

        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 中的方法:

  /// 

    /// 执行存储过程返回dataset

    /// 

    ///  存储过程名 

    ///  存储过程参数 

    ///  DataSet结果中的表名 

    ///  DataSet

    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;

        }

    }

    /// 

    /// 执行一条计算查询结果语句,返回查询结果(object)。

    /// 

    ///  计算查询结果语句 

    ///  查询结果(object) 

    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()方法的代码:

         /// 

        /// 根据传值进行搜索分页

        /// 

        /// 

        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)

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器