1、重写Repeater(修改过的,当只有一页的情况时,只显示Total页数及共有多少条目,不会显示上一页,下一页等):
View Code
using System.ComponentModel; using System.Web.UI; [assembly: TagPrefix("MyRepeater.Control", "MyRepeater")] namespace MyRepeater { /// <summary> /// JRepeater控件 /// </summary> [DefaultProperty("Text"), ToolboxData("<!--Repeater Begin --><div class=\"BigDiv_rep\"> <{0}:Repeater ID=\"Rep_List\" runat=server EnableViewState=\"false\" OnPreRender=\"Rep_List_PreRender\"> <HeaderTemplate><div class=\"BigDiv_Title_rep\"> <ul> <li style=\"line-height:22px;\"> <asp:CheckBox ID=\"TopCheckBox\" runat=\"server\" Text=\"ID\" EnableViewState=\"false\"/> </li><li style=\"10%;\"> </li> </ul> </div> </HeaderTemplate><ItemTemplate><div class=\"BigDiv_line_rep\" onmouseover=\"javascript:this.style.backgroundColor='#EEF8F9'\" onmouseout=\"javascript:this.style.backgroundColor='#FFFFFF'\"><ul ><li style=\"line-height:22px;\"><asp:CheckBox ID=\"ItemCheckBox\" runat=\"server\" Text='<%# Eval(\"ID\") %>' EnableViewState=\"false\"/></li><li style=\"10%;\"></li></ul></div></ItemTemplate> <FooterTemplate><div class=\"DelAllButton\"><asp:Button ID=\"btn_DelAll\" runat=\"server\" onclick=\"btn_DelAll_Click\" CssClass=\"btn_DelAllCss\" Text=\"删除\" EnableViewState=\"false\"/></div></FooterTemplate></{0}:Repeater></div><!--Repeater End -->")] public class Repeater : System.Web.UI.WebControls.Repeater { public bool ShowPagingWhenItemLessThanPageSize { get; set; } private int _recordcount = 0; private int _pagesize = 0; private string _pagelink = ""; private int _currentpage = 1; private string _urlSearchPrex = null; /// <summary> /// constructure /// </summary> public Repeater() { this.ShowPagingWhenItemLessThanPageSize = true; } #region _ [Bindable(true), Category("Data"), DefaultValue("1"), Description("totalCount")] public int RecordCount { get { return _recordcount; } set { _recordcount = value; } } [Bindable(true), Category("Data"), DefaultValue("1"), Description("pagesize")] public int PageSize { get { return _pagesize; } set { _pagesize = value; } } [Bindable(true), Category("Data"), DefaultValue(""), Description("currentLink")] public string PageLink { get { return _pagelink; } set { _pagelink = value; } } [Bindable(true), Category("Data"), DefaultValue("1"), Description("currentPageIndex")] public int CurrentPage { get { return _currentpage; } set { _currentpage = value; } } public string UrlSearchPrex { get { return _urlSearchPrex; } set { _urlSearchPrex = value; } } #endregion /// <summary> /// output control to page /// </summary> /// <param name="output"> </param> protected override void Render(HtmlTextWriter output) { base.Render(output); if (this._recordcount <= this._pagesize) { output.WriteLine("<div class=\"PageLink\"><font color=\"#666666\">Total:1 page(s) " + this._recordcount + " record(s)</div>"); return; } if (!(this._recordcount <= this._pagesize && !this.ShowPagingWhenItemLessThanPageSize)) { output.WriteLine("<div class=\"PageLink\">" + Pagination(_recordcount, _pagesize, _currentpage, _pagelink, _urlSearchPrex) + "</div>"); } } /// <summary> /// paging function /// </summary> /// <param name="recordcount">total count</param> /// <param name="pagesize"></param> /// <param name="currentpage"></param> /// <param name="url"></param> /// <returns></returns> public string Pagination(int recordcount, int pagesize, int currentpage, string url, string urlSearchPrex) { int allcurrentpage = 0; int next = 0; int pre = 0; int startcount = 0; int endcount = 0; string currentpagestr = ""; if (currentpage < 1) { currentpage = 1; } //total page count if (pagesize != 0) { allcurrentpage = (recordcount / pagesize); allcurrentpage = ((recordcount % pagesize) != 0 ? allcurrentpage + 1 : allcurrentpage); allcurrentpage = (allcurrentpage == 0 ? 1 : allcurrentpage); } next = currentpage + 1; pre = currentpage - 1; startcount = (currentpage + 5) > allcurrentpage ? allcurrentpage - 9 : currentpage - 4; endcount = currentpage < 5 ? 10 : currentpage + 5; if (startcount < 1) { startcount = 1; } if (allcurrentpage < endcount) { endcount = allcurrentpage; } currentpagestr = "<font color=\"#666666\">Total: " + allcurrentpage + " page(s) " + recordcount + " record(s) "; string checkaccountName = Page.Request.QueryString["checkaccount"]; string checkprojectName = Page.Request.QueryString["checkproject"]; urlSearchPrex += "&checkaccount=" + checkaccountName + "&checkproject=" + checkprojectName; string urlPrex = url + urlSearchPrex == null ? "?page=" : "?" + urlSearchPrex + "&page="; currentpagestr += currentpage > 1 ? "</font><a href=\"" + urlPrex + "1\"><span class=\"disabled\"> First </span></a> <a href=\"" + urlPrex + pre + "\"> <span class=\"disabled\"> < Prev </span></a>" : " <span class=\"disabled\"> First </span>"; for (int i = startcount; i <= endcount; i++) { currentpagestr += currentpage == i ? " <font color=\"#666666\"><span class=\"disabled\">" + "[ " + i + " ]" + "</span></font>" : " <a href=\"" + urlPrex + i + "\"><span class=\"ssss\">" + "[ " + i + " ]" + "</span></a> "; } currentpagestr += currentpage != allcurrentpage ? " <a href=\"" + urlPrex + next + "\"><span class=\"disabled\"> Next ></span></a> <a href=\"" + urlPrex + allcurrentpage + "\"> <span class=\"disabled\"> Last </span></a>" : " <font color=\"#666666\"><span class=\"disabled\"> Last </span></font>"; //currentpagestr += " <input id=\"Text1\" type=\"text\" />"; //currentpagestr += " <a href=\"javascript:CheckStatus();\">GO</a>"; return currentpagestr; } } }
2、页面调用方法:
View Code
<MyRepeater:Repeater ID="Rep_List" runat="server" EnableViewState="false" ShowPagingWhenItemLessThanPageSize="True" OnItemDataBound="Rep_List_ItemDataBound"> <HeaderTemplate> <table id="tbMain" width="100%" border="0" cellpadding="0" cellspacing="0" class="content_tb"> <thead> <tr> <th align="left" style="text-align: left"> <input type="checkbox" id="ckboxAll" /> </th> <th> <a href="#" id="aSortEmpNo" sort="EmpNo-ASC">Emp No.</a> </th> <th> <a href="#" id="aSortEmailID" sort="EmailID-ASC">Email ID</a> </th> <th> <a href="#" id="aSortRole" sort="Role-ASC">Role</a> </th> </tr> </thead> <tbody> </HeaderTemplate> <ItemTemplate> <tr class="search_tr" id="ResourceList" runat="server"> <td width="3%" style="white-space: nowrap;"> <input type="checkbox" value="<%# Eval("ID") %>" name="ckboxChild" style="display: inline" /> </td> <td width="10%" style="text-align: right;"> <%# Eval("EmpNo") %> <input type="hidden" value='' /> </td> <td width="10%" style="text-align: right;"> <%# Eval("EmailID") %> <input type="hidden" value='' /> </td> <td width="5%" class="ResourceStyle"> <%# Eval("Role") %> </td> </tr> </ItemTemplate> <FooterTemplate> </tbody> </table> </FooterTemplate> </MyRepeater:Repeater>
后台只需要把数据绑定;
3、常用高效数据库分页存储过程(借用的):
View Code
--高效分页存储过程 CREATE PROCEDURE [dbo].[GetListByPage]( @Table nvarchar(500), --表名 @Field nvarchar(500) = '*', --读取字段 @Where nvarchar(max) = NULL, --Where条件 @GroupBy nvarchar(500) = NULL, --分组 @OrderBy nvarchar(500)= NULL, --排序字段 @PrimaryKeyField nvarchar(50), --主键必需 @PageIndex int = 1, --开始页码 @PageSize int = 10, --页大小 @IsCount bit = 0 --返回记录总数 ------------------------------------------------------------------------------------------------ --当@IsCount为1时,将同时返回2张表,表0为记录总数,表1为查询结果 ------------------------------------------------------------------------------------------------ ) AS BEGIN ------------------------------------------------------------------------------------------------ DECLARE @strWhere nvarchar(500) --Where 条件 IF @Where IS NOT NULL AND @Where != '' --Where 条件 BEGIN SET @strWhere = ' WHERE ' + @Where + ' ' END ELSE BEGIN SET @strWhere = '' END ---------------------------------------------------------------------------------------------------- DECLARE @strGroupBy nvarchar(500) --GroupBy 条件 IF @GroupBy IS NOT NULL AND @GroupBy != '' --GroupBy 条件 BEGIN SET @strGroupBy = ' GROUP BY ' + @GroupBy + ' ' END ELSE BEGIN SET @strGroupBy = '' END ---------------------------------------------------------------------------------------------------- DECLARE @strOrderBy nvarchar(500) --OrderBy 条件 IF @OrderBy IS NULL OR @OrderBy = '' --OrderBy 条件 BEGIN SET @strOrderBy = ' ORDER BY ' + @PrimaryKeyField + ' DESC' END ELSE BEGIN SET @strOrderBy = ' ORDER BY ' + @OrderBy END ---------------------------------------------------------------------------------------------------- DECLARE @strSql nvarchar(max) --Sql 语句 --计算总行数 IF @IsCount = 1 BEGIN SET @strSql= 'SELECT Count (*) AS RecordCount FROM ' + @Table + @strWhere + @strGroupBy print @strSql EXEC sp_executesql @strSql --RETURN END ---------------------------------------------------------------------------------------------------- IF @PageIndex < 1 --第一页提高性能 BEGIN SET @PageIndex = 1 END IF @PageIndex = 1 BEGIN SET @strSql = 'SELECT TOP ' + str(@PageSize) + ' ' + @Field + ' FROM ' + @Table + @strWhere + @strGroupBy + @strOrderBy print @strSql EXEC sp_executesql @strSql RETURN END ---------------------------------------------------------------------------------------------------- DECLARE @STARTID nvarchar(50) DECLARE @ENDID nvarchar(50) SET @STARTID = convert(nvarchar(50),(@PageIndex - 1) * @PageSize + 1) SET @ENDID = convert(nvarchar(50),@PageIndex * @PageSize) SET @strSql = 'WITH MYTABLE AS (SELECT ROW_NUMBER() OVER (' + @strOrderBy + ') AS RowNumber,' + @Field + ' FROM '+ @Table + @strWhere + @strGroupBy + ') SELECT * FROM MYTABLE WHERE RowNumber BETWEEN ' + @STARTID + ' AND ' + @ENDID print @strSql EXEC sp_executesql @strSql -------------------------------------------------------------------------------------------------- END
4、有上面的存储过程的使用,同样可以在后台实现真分页