现在的位置: 首页 > 综合 > 正文

程序人生系列之新闻发布系统 我的分页经验

2012年08月29日 ⁄ 综合 ⁄ 共 2847字 ⁄ 字号 评论关闭

我的转正任务给部长看了之后,要求做一下真正的分页,而不是用 GridView 自带的分页功能,的确,我也觉得那个不好,那么就做吧,做了就可以转正了,哈哈哈!

---分页----

步骤一:存储过程 create PROCEDURE [dbo].[news_selectByIndex]

@startIndex int ,

@endIndex int

AS

BEGIN

     with temptbl as (

         SELECT ROW_NUMBER() OVER (ORDER BY id )AS Row,* from news 

     )

     SELECT * FROM temptbl where Row between @startIndex and @endIndex

END 

步骤二:在SQLHelper中添加相应的方法           

public SqlDataReader ExecuteNewsReader(CommandType cmdType, params SqlParameter[] cmdParms)

         {

             try

             {

                 conn = GetConn();

                 string cmdText = "news_selectByIndex";

                 cmd = new SqlCommand(cmdText,conn);

                 cmd.CommandType = cmdType;

                 cmd.Parameters.AddRange(cmdParms);

                 SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                 cmd.Parameters.Clear();

                 return rdr;

             }

             catch

             {

                 conn.Close();

                 throw;

             }

         }  

步骤三:在页面中添加 ASPNETPager 控件  

页首添加 

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %> 

<webdiyer:AspNetPager ID="AspNetPager1" CssClass="paginator" CurrentPageButt runat="server" AlwaysShow="True" FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PageSize="8" PrevPageText="上一页" ShowCustomInfoSection="Left" ShowInputBox="Never"  CustomInfoTextAlign="Left" LayoutType="Table" >

</webdiyer:AspNetPager>

步骤四:为 ASPNETPager 控件添加 CSS 属性

.paginator

{

     font: 12px Arial, Helvetica, sans-serif;

     padding: 10px 20px 10px 0;

     margin: 0px;

}

.paginator a

{

     border: solid 1px #ccc;

     color: #0063dc;

     cursor: pointer;

     text-decoration: none;

}

.paginator a:visited

{

     padding: 1px 6px;

     border: solid 1px #ddd;

     background: #fff;

     text-decoration: none;

}

.paginator .cpb

{

     border: 1px solid #F50;

     font-weight: 700;

     color: #F50;

     background-color: #ffeee5;

}

.paginator a:hover

{

     border: solid 1px #F50;

     color: #f60;

     text-decoration: none;

}

.paginator a, .paginator a:visited, .paginator .cpb, .paginator a:hover

{

     float: left;

     height: 16px;

     line-height: 16px;

     min-width: 10px;

     _width: 10px;

     margin-right: 5px;

     text-align: center;

     white-space: nowrap;

     font-size: 12px;

     font-family: Arial,SimSun;

     padding: 0 3px;

}

步骤五:编写页面后台代码 

  public void BindData()

    {

        //绑定语句

         repNews.DataSource = new SQLHelper().ExecuteNewsReader(CommandType.StoredProcedure,

             new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),

             new SqlParameter("@endIndex", AspNetPager1.EndRecordIndex));

         repNews.DataBind();

        this.AspNetPager1.CustomInfoHTML = string.Format("当前第{0}/{1}页 共{2}条记录 每页{3}条", new object[] { this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageCount, this.AspNetPager1.RecordCount, this.AspNetPager1.PageSize });

    }

     protected void AspNetPager1_PageChanged(object src, EventArgs e)

     {

         BindData();

     }

然后修改后台代码的各个地方,需要使用 BindDate() 的时候都改过来,例如 Page_Load() 还有删除了新闻分类后等等

抱歉!评论已关闭.