//此源代碼僅供學習參考,不得用作任何商業用途;
//若需修改並重新編譯該控件,請保留完整的源代碼的版權信息!
//有關控件升級及新控件發布信息,請留意www.webdiyer.com 。
using system;
using system.io;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.collections.specialized;
using system.text;
using system.componentmodel;
using system.componentmodel.design;
using system.collections;
namespace wuqi.webdiyer
{
#region aspnetpager server control
#region 控件說明及示例
/// <summary>
/// 用於asp.net web應用程序中對數據進行分頁的的服務器控件。
/// </summary>
/// <remarks>不同於datagrid控件,aspnetpager分頁控件本身並不顯示任何數據,而只顯示頁導航元素,數據在頁面上的顯示方式與該控件無關。該控件可以為datagrid、datalist、repeater以及自定義控件進行分頁,配合sql存儲過程,分頁性能較使用datagrid分頁有明顯提升,尤其是當數據量大時性能可提升數倍!
/// <p>aspnetpager 2.0 中新增了通過url來分頁的功能,這使得訪問者可以直接輸入相應的url來訪問任何頁面,並且搜索引擎也可以直接檢索每個頁面,若使用datagrid的分頁功能,這是無法實現的。 </p>
/// <p>要使用aspnetpager 分頁控件,必須最少指定它的<see cref="recordcount"/> 屬性,指定並編寫<see cref="pagechanged"/> 事件的處理程序。
/// <see cref="recordcount"/> 屬性指定要分頁的所有數據的總項數,若未指定該值或該值小於等於<see cref="pagesize"/> ,則aspnetpager控件不會顯示任何內容。
/// 若未指定並編寫<see cref="pagechanged"/> 事件處理程序,則當用戶點擊頁導航元素或在頁索引文本框中手式輸入頁索引並提交時aspnetpager不會跳轉到指定的頁。
/// aspnetpager控件的分頁方法和datagrid基本相同,即在它的<see cref="pagechanged"/> 事件處理程序中將傳遞事件數據的<see cref="pagechangedeventargs"/> 的<see cref=" pagechangedeventargs.newpageindex"/>值賦給aspnetpager的<see cref="currentpageindex"/>屬性,然後重新將新的數據與數據顯示控件綁定。 </p></remarks>
/// <example>以下示例說明如何用aspnetpager對datagrid進行分頁。
/// <code><![cdata[
///<%@ page language="c#"%>
///<%@ import namespace="system.data"%>
///<%@import namespace="system.data.sqlclient"%>
///<%@import namespace="system.configuration"%>
///<%@register tagprefix="webdiyer" namespace="wuqi.webdiyer" assembly="aspnetpager"%>
///<html>
///<head>
///<title>welcome to webdiyer.com </title>
/// <script runat="server">
/// sqlconnection conn;
/// sqlcommand cmd;
/// void page_load(object src,eventargs e)
/// {
/// conn=new sqlconnection(configurationsettings.appsettings["connstr"]);
/// if(!page.ispostback)
/// {
/// cmd=new sqlcommand("getnews",conn);
/// cmd.commandtype=commandtype.storedprocedure;
/// cmd.parameters.add("@pageindex",1);
/// cmd.parameters.add("@pagesize",1);
/// cmd.parameters.add("@docount",true);
/// conn.open();
/// pager.recordcount=(int)cmd.executescalar();
/// conn.close();
/// binddata();
/// }
/// }
///
/// void binddata()
/// {
/// cmd=new sqlcommand("getnews",conn);
/// cmd.commandtype=commandtype.storedprocedure;
/// cmd.parameters.add("@pageindex",pager.currentpageindex);
/// cmd.parameters.add("@pagesize",pager.pagesize);
/// cmd.parameters.add("@docount",false);
/// conn.open();
/// datagrid1.datasource=cmd.executereader();
/// datagrid1.databind();
/// conn.close();
/// pager.custominfotext="記錄總數:<font color="blue"><b>"+pager.recordcount.tostring()+"</b></font>";
/// pager.custominfotext+=" 總頁數:<font color="blue"><b>"+pager.pagecount.tostring()+"</b></font>";
/// pager.custominfotext+=" 當前頁:<font color="red"><b>"+pager.currentpageindex.tostring()+"</b></font>";
/// }
/// void changepage(object src,pagechangedeventargs e)
/// {
/// pager.currentpageindex=e.newpageindex;
/// binddata();
/// }
/// </script>
/// <meta http-equiv="content-language" content="zh-cn">
/// <meta http-equiv="content-type" content="text/html;charset=gb2312">
/// <meta name="generator" content="editplus">
/// <meta name="author" content="webdiyer([email protected])">
/// </head>
/// <body>
/// <form runat="server" id="form1">
/// <asp:datagrid id="datagrid1" runat="server" />
/// <webdiyer:aspnetpager id="pager"
/// runat="server"
/// pagesize="8"
/// numericbuttoncount="8"
/// showcustominfosection="left"
/// pagingbuttonspacing="0"
/// showinputbox="always"
/// cssclass="mypager"
/// horizontalalign="right"
/// onpagechanged="changepage"
/// submitbuttontext="轉到"
/// numericbuttontextformatstring="[{0}]"/>
/// </form>
/// </body>
///</html>
/// ]]>
/// </code>
/// <p>下面是該示例所用的sql server存儲過程:</p>
/// <code>
/// <![cdata[
///create procedure getnews
/// (@pagesize int,
/// @pageindex int,
/// @docount bit)
/// as
/// set nocount on
/// if(@docount=1)
/// select count(id) from news
/// else
/// begin
/// declare @indextable table(id int identity(1,1),nid int)
/// declare @pagelowerbound int
/// declare @pageupperbound int
/// set @pagelowerbound=(@pageindex-1)*@pagesize
/// set @[email protected][email protected]
/// set rowcount @pageupperbound
/// insert into @indextable(nid) select id from news order by addtime desc
/// select o.id,o.source,o.title,o.addtime from news o,@indextable t where o.id=t.nid
/// and t.id>@pagelowerbound and t.id < = @pageupperbound order by t.id
/// end
/// set nocount off
///go
/// ]]>
/// </code></example>
#endregion
[defaultproperty("pagesize")]
[defaultevent("pagechanged")]
[parsechildren(false)]
[persistchildren(false)]
[description("專用於asp.net web應用程序的分頁控件")]
[designer(typeof(pagerdesigner))]
[toolboxdata("<{0}:aspnetpager runat=server></{0}:aspnetpager>")]
public class aspnetpager:panel,inamingcontainer,ipostbackeventhandler,ipostbackdatahandler
{
private string cssclassname;
private string urlpageindexname="page";
private bool urlpaging=false;
private string inputpageindex;
private string currenturl=null;
private namevaluecollection urlparams=null; #region properties #region navigation buttons /// <summary>
/// 獲取或設置一個值,該值批示當鼠標指針懸停在導航按鈕上時是否顯示工具提示。
/// </summary>
[browsable(true),
category("導航按鈕"), defaultvalue(true), description("指定當鼠標停留在導航按鈕上時,是否顯示工具提示")]
public bool shownavigationtooltip
{
get
{
object obj=viewstate["shownavigationtooltip"];
return (obj==null)?true:(bool)obj;
}
set
{
viewstate["shownavigationtooltip"]=value;
}
} /// <summary>
/// 獲取或設置導航按鈕工具提示文本的格式。
/// </summary>
[browsable(true),
category("導航按鈕"),
defaultvalue("轉到第{0}頁"),
description("頁導航按鈕工具提示文本的格式")]
public string navigationtooltiptextformatstring
{
get
{
object obj=viewstate["navigationtooltiptextformatstring"];
return (obj==null)?"轉到第{0}頁":(string)obj;
}
set
{
string tip=value;
if(tip.trim().length<1&&tip.indexof("{0}")<0)
tip="{0}";
viewstate["navigationtooltiptextformatstring"]=tip;
}
} /// <summary>
/// 獲取或設置一個值,該值指示是否將頁索引按鈕用中文數字代替。
/// </summary>
/// <remarks>
/// 將該值設為true並且未使用圖片按鈕時,頁索引按鈕中的數值1、2、3等將會被中文字符一、二、三等代替。
/// </remarks>
[browsable(true),
category("導航按鈕"),
defaultvalue(false),
description("是否將頁索引數值按鈕用中文數字一、二、三等代替")]
public bool chinesepageindex
{
get
{
object obj=viewstate["chinesepageindex"];
return (obj==null)?false:(bool)obj;
}
set
{
viewstate["chinesepageindex"]=value;
}
} /// <summary>
/// 獲取或設置頁索引數值導航按鈕上文字的顯示格式。
/// </summary>
/// <value>
/// 字符串,指定頁索引數值按鈕上文字的顯示格式,默認值為<see cref="string.empty"/>,即未設置該屬性。 </value>
/// <remarks>
/// 使用numericbuttontextformatstring屬性指定頁索引數值按鈕的顯示格式,如未設置該值時索引按鈕文本將會是:1 2 3 ...,設置該值將改變索引按鈕文本的顯示格式,
/// 如將該值設為“[{0}]”則索引文本會顯示為:[1] [2] [3] ...,將該值設為“-{0}-”則會使索引文本變為:-1- -2- -3- ...。
/// </remarks>
[browsable(true),
defaultvalue(""),
category("導航按鈕"),
description("頁索引數值按鈕上文字的顯示格式")]
public string numericbuttontextformatstring
{
get
{
object obj=viewstate["numericbuttontextformatstring"];
return (obj==null)?string.empty:(string)obj;
}
set
{
viewstate["numericbuttontextformatstring"]=value;
}
} /// <summary>
/// 獲取或設置分頁導航按鈕的類型,即使用文字還是圖片。
/// </summary>
/// <remarks>
/// 要使用圖片按鈕,您需要準備以下圖片:從0到9的十個數值圖片(當showpageindex設為true時),第一頁、上一頁、下一頁、最後一頁及更多頁(...)五個按鈕圖片(當showfirstlast及showprevnext都設為true時),
/// 若需要使當前頁索引的數值按鈕不同於別的頁索引數值按鈕,則還需準備當前頁索引的按鈕圖片;
/// 若需要使已禁用的第一頁、上一頁、下一頁及最後一頁按鈕圖片不同於正常的按鈕圖片,則還需準備這四個按鈕在禁用狀態下的圖片;
/// <p><b>圖片文件的命名規則如下:</b></p>
/// <p>從0到9十張數值按鈕圖片必須命名為“數值+buttonimagenameextension+buttonimageextension”,其中的buttonimagenameextension可以不用設置,
/// buttonimageextension是圖片文件的後綴名,如.gif或.jpg等可以在瀏覽器中顯示的任何圖片文件類型。如頁索引“1”的圖片文件可命名為“1.gif”或“1.jpg”,
/// 當您有兩套或更多套圖片文件時,可以通過指定buttonimagenameextension屬性值來區分不同套的圖片,如第一套圖片可以不用設buttonimagenameextension,則圖片文件名類似於“1.gif” 、“2.gif”等等,而第二套圖片則設置buttonimagenameextension為“f”,圖片文件名類似於“1f.gif”,“2f.gif”等等。 </p>
/// <p>第一頁按鈕的圖片文件名以“first”開頭,上一頁按鈕圖片名以“prev”開頭,下一頁按鈕圖片名以“next”開頭,最後一頁按鈕圖片名以“last”開頭,更多頁按鈕圖片名以“more”開頭,是否使用buttonimagenameextension取決於數值按鈕的設置及是否有更多套圖片。 </p>
/// </remarks>
/// <example>
/// 以下代碼片段示例如果使用圖片按鈕:
/// <p>
/// <code><![cdata[
/// <webdiyer:aspnetpager runat="server"
/// id="pager1"
/// onpagechanged="changepage"
/// pagingbuttontype="image"
/// imagepath="images"
/// buttonimagenameextension="n"
/// disabledbuttonimagenameextension="g"
/// buttonimageextension="gif"
/// cpibuttonimagenameextension="r"
/// pagingbuttonspacing=5/>
/// ]]>
/// </code>
/// </p>
/// </example>
[browsable(true),
defaultvalue(pagingbuttontype.text),
category("導航按鈕"),
description("分頁導航按鈕的類型,是使用文字還是圖片")]
public pagingbuttontype pagingbuttontype
{
get
{
object obj=viewstate["pagingbuttontype"];
return (obj==null)?pagingbuttontype.text:(pagingbuttontype)obj;
}
set
{
viewstate["pagingbuttontype"]=value;
}
} /// <summary>
/// 獲取或設置頁導航數值按鈕的類型,該值僅當pagingbuttontype設為image時才有效。
/// </summary>
/// <remarks>
/// 當您將pagingbuttontype設為image當又不想讓頁索引數值按鈕使用圖片時,可以將該值設為text,這會使頁索引數據按鈕使用文本而不是圖片按鈕。
/// </remarks>
[browsable(true),
defaultvalue(pagingbuttontype.text),
category("導航按鈕"),
description("頁導航數值按鈕的類型")]
public pagingbuttontype numericbuttontype
{
get
{
object obj=viewstate["numericbuttontype"];
return (obj==null)?pagingbuttontype:(pagingbuttontype)obj;
}
set
{
viewstate["numericbuttontype"]=value;
}
} /// <summary>
/// 獲取或設置第一頁、上一頁、下一頁和最後一頁按鈕的類型,該值僅當pagingbuttontype設為image時才有效。
/// </summary>
/// <remarks>
/// 當您將pagingbuttontype設為image但又不想讓第一頁、下一頁、下一頁和最後一頁按鈕使用圖片,則可以將該值設為text,這會使前面的四個按鈕使用文本而不是圖片按鈕。
/// </remarks>
[browsable(true),
category("導航按鈕"),
defaultvalue(pagingbuttontype.text),
description("第一頁、上一頁、下一頁和最後一頁按鈕的類型")]
public pagingbuttontype navigationbuttontype
{
get
{
object obj=viewstate["navigationbuttontype"];
return (obj==null)?pagingbuttontype:(pagingbuttontype)obj;
}
set
{
viewstate["navigationbuttontype"]=value;
}
} /// <summary>
/// 獲取或設置“更多頁”(...)按鈕的類型,該值僅當pagingbuttontype設為image時才有效。
/// </summary>
/// <remarks>
/// 當您將pagingbuttontype設為image但又不想讓更多頁(...)按鈕使用圖片時,可以將此值設為text,這會使更多頁按鈕使用文本而不是圖片按鈕。
/// </remarks>
[browsable(true),
category("導航按鈕"),
defaultvalue(pagingbuttontype.text),
description("“更多頁”(...)按鈕的類型")]
public pagingbuttontype morebuttontype
{
get
{
object obj=viewstate["morebuttontype"];
return (obj==null)?pagingbuttontype:(pagingbuttontype)obj;
}
set
{
viewstate["morebuttontype"]=value;
}
} /// <summary>
/// 獲取或設置分頁導航按鈕之間的間距。
/// </summary>
[browsable(true),
category("導航按鈕"),
defaultvalue(typeof(unit),"5px"),
description("分頁導航按鈕之間的間距")]
public unit pagingbuttonspacing
{
get
{
object obj=viewstate["pagingbuttonspacing"];
return (obj==null)?unit.pixel(5):(unit.parse(obj.tostring()));
}
set
{
viewstate["pagingbuttonspacing"]=value;
}
} /// <summary>
/// 獲取或設置一個值,該值指示是否在頁導航元素中顯示第一頁和最後一頁按鈕。
/// </summary>
[browsable(true),
description("是否在頁導航元素中顯示第一頁和最後一頁按鈕"),
category("導航按鈕"),
defaultvalue(true)]
public bool showfirstlast
{
get
{
object obj=viewstate["showfirstlast"];
return (obj==null)?true:(bool)obj;
}
set{viewstate["showfirstlast"]=value;}
} /// <summary>
/// 獲取或設置一個值,該值指示是否在頁導航元素中顯示上一頁和下一頁按鈕。
/// </summary>
[browsable(true),
description("是否在頁導航元素中顯示上一頁和下一頁按鈕"),
category("導航按鈕"),
defaultvalue(true)]
public bool showprevnext
{
get
{
object obj=viewstate["showprevnext"];
return (obj==null)?true:(bool)obj;
}
set{viewstate["showprevnext"]=value;}
} /// <summary>
/// 獲取或設置一個值,該值指示是否在頁導航元素中顯示頁索引數值按鈕。
/// </summary>
[browsable(true),
description("是否在頁導航元素中顯示數值按鈕"),
category("導航按鈕"),
defaultvalue(true)]
public bool showpageindex
{
get
{
object obj=viewstate["showpageindex"];
return (obj==null)?true:(bool)obj;
}
set{viewstate["showpageindex"]=value;}
} /// <summary>
/// 獲取或設置為第一頁按鈕顯示的文本。
/// </summary>
[browsable(true),
description("第一頁按鈕上顯示的文本"),
category("導航按鈕"),
defaultvalue("<font face="webdings">9</font>")]
public string firstpagetext
{
get
{
object obj=viewstate["firstpagetext"];
return (obj==null)?"<font face="webdings">9</font>":(string)obj;
}
set{viewstate["firstpagetext"]=value;}
} /// <summary>
/// 獲取或設置為上一頁按鈕顯示的文本。
/// </summary>
[browsable(true),
description("上一頁按鈕上顯示的文本"),
category("導航按鈕"),
defaultvalue("<font face="webdings">3</font>")]
public string prevpagetext
{
get
{
object obj=viewstate["prevpagetext"];
return (obj==null)?"<font face="webdings">3</font>":(string)obj;
}
set{viewstate["prevpagetext"]=value;}
} /// <summary>
/// 獲取或設置為下一頁按鈕顯示的文本。
/// </summary>
[browsable(true),
description("下一頁按鈕上顯示的文本"),
category("導航按鈕"),
defaultvalue("<font face="webdings">4</font>")]
public string nextpagetext
{
get
{
object obj=viewstate["nextpagetext"];
return (obj==null)?"<font face="webdings">4</font>":(string)obj;
}
set{viewstate["nextpagetext"]=value;}
} /// <summary>
/// 獲取或設置為最後一頁按鈕顯示的文本。
/// </summary>
[browsable(true),
description("最後一頁按鈕上顯示的文本"),
category("導航按鈕"),
defaultvalue("<font face="webdings">:</font>")]
public string lastpagetext
{
get
{
object obj=viewstate["lastpagetext"];
return (obj==null)?"<font face="webdings">:</font>":(string)obj;
}
set{viewstate["lastpagetext"]=value;}
} /// <summary>
/// 獲取或設置在<see cref="aspnetpager"/> 控件的頁導航元素中同時顯示的數值按鈕的數目。
/// </summary>
[browsable(true),
description("要顯示的頁索引數值按鈕的數目"),
category("導航按鈕"),
defaultvalue(10)]
public int numericbuttoncount
{
get
{
object obj=viewstate["numericbuttoncount"];
return (obj==null)?10:(int)obj;
}
set{viewstate["numericbuttoncount"]=value;}
} /// <summary>
/// 獲取或設置一個值,該值指定是否顯示已禁用的按鈕。
/// </summary>
/// <remarks>
/// 該值用來指定是否顯示已禁用的分頁導航按鈕,噹噹前頁為第一頁時,第一頁和上一頁按鈕將被禁用,噹噹前頁為最後一頁時,下一頁和最後一頁按鈕將被禁用,被禁用的按鈕沒有鏈接,在按鈕上點擊也不會有任何作用。
/// </remarks>
[browsable(true),
category("導航按鈕"),
description("是否顯示已禁用的按鈕"),
defaultvalue(true)]
public bool showdisabledbuttons
{
get
{
object obj=viewstate["showdisabledbuttons"];
return (obj==null)?true:(bool)obj;
}
set
{
viewstate["showdisabledbuttons"]=value;
}
}
#endregion
#region image buttons
/// <summary>
/// 獲取或設置當使用圖片按鈕時,圖片文件的路徑。
/// </summary>
[browsable(true),
category("圖片按鈕"),
description("當使用圖片按鈕時,指定圖片文件的路徑"),
defaultvalue(null)]
public string imagepath
{
get
{
string imgpath=(string)viewstate["imagepath"];
if(imgpath!=null)
imgpath=this.resolveurl(imgpath);
return imgpath;
}
set
{
string imgpath=value.trim().replace("","/");
viewstate["imagepath"]=(imgpath.endswith("/"))?imgpath:imgpath+"/";
}
}
/// <summary>
/// 獲取或設置當使用圖片按鈕時,圖片的類型,如gif或jpg,該值即圖片文件的後綴名。
/// </summary>
[browsable(true),
category("圖片按鈕"),
defaultvalue(".gif"),
description("當使用圖片按鈕時,圖片的類型,如gif或jpg,該值即圖片文件的後綴名")]
public string buttonimageextension
{
get
{
object obj=viewstate["buttonimageextension"];
return (obj==null)?".gif":(string)obj;
}
set
{
string ext=value.trim();
viewstate["buttonimageextension"]=(ext.startswith("."))?ext:("."+ext);
}
}
/// <summary>
/// 獲取或設置自定義圖片文件名的後綴字符串,以區分不同類型的按鈕圖片。
/// </summary>
/// <remarks><note>注意:</note>該值不是文件後綴名,而是為區分不同的圖片文件而在圖片名中加入的字符串,如:
/// 當前有兩套按鈕圖片,其中一套中的“1”的圖片名可為“1f.gif”,另一套中的“1”的圖片名可起為“1n.gif”,其中的f和n即為buttonimagenameextension。 </remarks>
[browsable(true),
defaultvalue(null),
category("圖片按鈕"),
description("自定義圖片文件名的後綴字符串(非文件後綴名),如圖片“1f.gif”的buttonimagenameextension即為“f”")]
public string buttonimagenameextension
{
get
{
return (string)viewstate["buttonimagenameextension"];
}
set
{
viewstate["buttonimagenameextension"]=value;
}
}
/// <summary>
/// 獲取或設置當前頁索引按鈕的圖片名後綴。
/// </summary>
/// <remarks>
/// 當<see cref="pagingbuttontype"/> 設為image 時,該屬性允許您設置當前頁索引數值按鈕使用的圖片名後綴字符,因此可以使當前頁索引按鈕與其它頁索引按鈕使用不同的圖片,若未設置該值,則默認值為<see cref="buttonimagenameextension"/>,即當前頁索引按鈕與其它頁索引按鈕使用相同的圖片。
/// </remarks>
[browsable(true),
defaultvalue(null),
category("圖片按鈕"),
description("當前頁索引按鈕的圖片名後綴字符串")]
public string cpibuttonimagenameextension
{
get
{
object obj=viewstate["cpibuttonimagenameextension"];
return (obj==null)?buttonimagenameextension:(string)obj;
}
set
{
viewstate["cpibuttonimagenameextension"]=value;
}
}
/// <summary>
/// 獲取或設置已禁用的頁導航按鈕圖片名後綴字符串。
/// </summary>
/// <remarks>
/// 當<see cref="pagingbuttontype"/> 設為image 時, 該值允許您設置已禁用(即沒有鏈接,因而點擊後無反應)的頁導航按鈕(包括第一頁、上一頁、下一頁、最後一頁四個按鈕)的圖片文件名後綴字符串,因此可以使已禁用的頁導航按鈕不同於正常的頁導航按鈕。若未設置該值,則默認值為<see cref="buttonimagenameextension"/>,即已禁用的頁導航按鈕與正常的頁導航按鈕使用相同的圖片。
/// </remarks>
[browsable(true),
defaultvalue(null),
category("圖片按鈕"),
description("已禁用的頁導航按鈕的圖片名後綴字符串")]
public string disabledbuttonimagenameextension
{
get
{
object obj=viewstate["disabledbuttonimagenameextension"];
return (obj==null)?buttonimagenameextension:(string)obj;
}
set
{
viewstate["disabledbuttonimagenameextension"]=value;
}
}
/// <summary>
/// 指定當使用圖片按鈕時,圖片的對齊方式。
/// </summary>
[browsable(true),
description("指定當使用圖片按鈕時,圖片的對齊方式"),
defaultvalue(imagealign.baseline),
category("圖片按鈕")]
public imagealign buttonimagealign
{
get
{
object obj=viewstate["buttonimagealign"];
return (obj==null)?imagealign.baseline:(imagealign)obj;
}
set{viewstate["buttonimagealign"]=value;}
}
#endregion
#region paging
/// <summary>
/// 獲取或設置是否啟用url來傳遞分頁信息。
/// </summary>
/// <remarks>
/// 啟用url分頁方式是將用戶欲訪問的頁索引通過url來傳遞,由於該分頁方式不使用頁面向自身回發來傳遞數據,
/// 所以每次分頁時所有的數據都恢復為初始值或需要重新獲取。使用url分頁方式不支持動態改變分頁控件的屬性值,
/// 因暫時無法將新的屬性值通過url來傳遞給下一頁。
/// </remarks>
/// <example>以下示例說明如何用aspnetpager的url分頁方式對datagrid進行分頁(使用access數據庫):
/// <code><![cdata[
///<%@register tagprefix="webdiyer" namespace="wuqi.webdiyer" assembly="aspnetpager"%>
///<%@import namespace="system.data.oledb"%>
///<%@ import namespace="system.data"%>
///<%@ page language="c#" debug=true%>
///<html>
/// <head>
/// <title>welcome to webdiyer.com </title>
/// <script runat="server">
/// oledbconnection conn;
/// oledbcommand cmd;
/// void page_load(object src,eventargs e){
/// conn=new oledbconnection("provider=microsoft.jet.oledb.4.0;data source="+server.mappath("access/aspnetpager.mdb"));
/// if(!page.ispostback){
/// cmd=new oledbcommand("select count(newsid) from wqnews",conn);
/// conn.open();
/// pager.recordcount=(int)cmd.executescalar();
/// conn.close();
/// binddata();
/// }
/// }
///
/// void binddata(){
/// cmd=new oledbcommand("select newsid,heading,source,addtime from wqnews order by addtime desc",conn);
/// oledbdataadapter adapter=new oledbdataadapter(cmd);
/// dataset ds=new dataset();
/// adapter.fill(ds,pager.pagesize*(pager.currentpageindex-1),pager.pagesize,"news");
/// dg.datasource=ds.tables["news"];
/// dg.databind();
/// }
///
/// void changepage(object src,pagechangedeventargs e){
/// pager.currentpageindex=e.newpageindex;
/// binddata();
/// }
///
/// </script>
/// <meta http-equiv="content-language" content="zh-cn">
/// <meta http-equiv="content-type" content="text/html;charset=gb2312">
/// <meta name="generator" content="editplus">
/// <meta name="author" content="webdiyer([email protected])">
/// </head>
/// <body>
/// <form runat="server" id="form1">
/// <h2 align="center">aspnetpager分頁示例</h2>
/// <asp:datagrid id="dg" runat="server"
/// cellpadding="4" align="center" />
///
/// <webdiyer:aspnetpager runat="server" id="pager"
/// onpagechanged="changepage"
/// horizontalalign="center"
///
/// pagesize="8"
/// showinputbox="always"
/// submitbuttonstyle="border:1px solid #000066;height:20px;width:30px"
/// inputboxstyle="border:1px #0000ff solid;text-align:center"
/// submitbuttontext="轉到"
/// urlpaging="true"
/// urlpageindexname="pageindex" />
/// </form>
/// </body>
///</html>
/// ]]></code>
/// </example>
[browsable(true),
category("分頁"),
defaultvalue(false),
description("是否使用url傳遞分頁信息的方式來分頁")]
public bool urlpaging
{
get
{
return urlpaging;
}
set
{
urlpaging=value;
}
}
/// <summary>
/// 獲取或設置當啟用url分頁方式時,在url中表示要傳遞的頁索引的參數的名稱。
/// </summary>
/// <remarks>
/// 該屬性允許您自定義通過url傳遞頁索引時表示要傳遞的頁索引的參數的名稱,以避免與現有的參數名重複。
/// <p>該屬性的默認值是“page”,即通過url分頁時,顯示在瀏覽器地址欄中的url類似於:</p>http://www.webdiyer.com/aspnetpager/ samples/datagrid_url.aspx?page=2
/// <p>如將該值改為“pageindex”,則上面的url將變為:</p><p>http://www.webdiyer.com/aspnetpager/samples/datagrid_url.aspx?pageindex =2 </p>
/// </remarks>
[browsable(true),
defaultvalue("page"),
category("分頁"),
description("當啟用url分頁方式時,顯示在url中表示要傳遞的頁索引的參數的名稱")]
public string urlpageindexname
{
get{return urlpageindexname;}
set{urlpageindexname=value;}
}
/// <summary>
/// 獲取或設置當前顯示頁的索引。
/// </summary>
///<remarks>使用此屬性來確定在aspnetpager 控件中當前顯示的頁,當前顯示的頁的數字索引將以紅色字體加粗顯示。此屬性還用於以編程的方式控制所顯示的頁。
///<p> <b>注意:</b>不同於datagrid控件的currentpageindex,aspnetpager的currentpageindex屬性是從1開始的。 </p></remarks>
[readonly(true),
browsable(false),
description("當前顯示頁的索引"),
category("分頁"),
defaultvalue(1),
designerserializationvisibility(designerserializationvisibility.hidden)]
public int currentpageindex
{
get
{
object cpage=viewstate["currentpageindex"];
int pindex=(cpage==null)?1:(int)cpage;
if(pindex>pagecount&&pagecount>0)
return pagecount;
else if(pindex<1)
return 1;
return pindex;
}
set
{
int cpage=value;
if(cpage<1)
cpage=1;
else if(cpage>this.pagecount)
cpage=this.pagecount;
viewstate["currentpageindex"]=cpage;
}
}
/// <summary>
/// 獲取或設置需要分頁的所有記錄的總數。
/// </summary>
/// <remarks>
/// 當頁面第一次加載時,應以編程方式將從存儲過程或sql語句中返回的數據表中所有要分頁的記錄的總數賦予該屬性,aspnetpager會將其保存的viewstate中並在頁面回發時從viewstate中獲取該值,因此避免了每次分頁都要訪問數據庫而影響分頁性能。 aspnetpager根據要分頁的所有數據的總項數和<see cref="pagesize"/> 屬性來計算顯示所有數據需要的總頁數,即<see cref="pagecount"/>的值。
/// </remarks>
/// <example>
/// 下面的示例顯示如何以編程方式將從sql語句返回的記錄總數賦給該屬性:
/// <p>
/// <code><![cdata[
/// <html>
/// <head>
/// <title>welcome to webdiyer.com </title>
/// <script runat="server">
/// sqlconnection conn;
/// sqlcommand cmd;
/// void page_load(object src,eventargs e)
/// {
/// conn=new sqlconnection(configurationsettings.appsettings["connstr"]);
/// if(!page.ispostback)
/// {
/// cmd=new sqlcommand("select count(id) from news",conn);
/// conn.open();
/// pager.recordcount=(int)cmd.executescalar();
/// conn.close();
/// binddata();
/// }
/// }
///
/// void binddata()
/// {
/// cmd=new sqlcommand("getpagednews",conn);
/// cmd.commandtype=commandtype.storedprocedure;
/// cmd.parameters.add("@pageindex",pager.currentpageindex);
/// cmd.parameters.add("@pagesize",pager.pagesize);
/// conn.open();
/// datagrid1.datasource=cmd.executereader();
/// datagrid1.databind();
/// conn.close();
/// }
/// void changepage(object src,pagechangedeventargs e)
/// {
/// pager.currentpageindex=e.newpageindex;
/// binddata();
/// }
/// </script>
/// <meta http-equiv="content-language" content="zh-cn">
/// <meta http-equiv="content-type" content="text/html;charset=gb2312">
/// <meta name="generator" content="editplus">
/// <meta name="author" content="webdiyer([email protected])">
/// </head>
/// <body>
/// <form runat="server" id="form1">
/// <asp:datagrid id="datagrid1" runat="server" />
///
/// <webdiyer:aspnetpager id="pager" runat="server"
/// pagesize="8"
/// numericbuttoncount="8"
/// showcustominfosection="before"
/// showinputbox="always"
/// cssclass="mypager"
/// horizontalalign="center"
/// onpagechanged="changepage" />
///
/// </form>
/// </body>
///</html>
/// ]]>
/// </code></p>
/// <p>本示例使用的存儲過程代碼如下:</p>
/// <code><![cdata[
///create procedure getpagednews
/// (@pagesize int,
/// @pageindex int)
/// as
/// set nocount on
/// declare @indextable table(id int identity(1,1),nid int)
/// declare @pagelowerbound int
/// declare @pageupperbound int
/// set @pagelowerbound=(@pageindex-1)*@pagesize
/// set @[email protected][email protected]
/// set rowcount @pageupperbound
/// insert into @indextable(nid) select id from news order by addtime desc
/// select o.id,o.title,o.source,o.addtime from news o,@indextable t where o.id=t.nid
/// and t.id>@pagelowerbound and t.id<[email protected] order by t.id
/// set nocount off
///go
/// ]]>
/// </code>
/// </example>
[browsable(false),
description("要分頁的所有記錄的總數,該值須在程序運行時設置,默認值為225是為設計時支持而設置的參照值。"),
category("data"),
defaultvalue(225)]
public int recordcount
{
get
{
object obj=viewstate["recordcount"];
return (obj==null)?0:(int)obj;
}
set{viewstate["recordcount"]=value;}
}
/// <summary>
/// 獲取當前頁之後未顯示的頁的總數。
/// </summary>
[browsable(false),
designerserializationvisibility(designerserializationvisibility.hidden)]
public int pagesremain
{
get
{
return pagecount-currentpageindex;
}
}
/// <summary>
/// 獲取或設置每頁顯示的項數。
/// </summary>
/// <remarks>
/// 該值獲取或設置數據呈現控件每次要顯示數據表中的的數據的項數,aspnetpager根據該值和<see cref="recordcount"/> 來計算顯示所有數據需要的總頁數,即<see cref="pagecount"/>的值。 </remarks>
/// <example>以下示例將<see cref="aspnetpager"/> 設置為允許每頁顯示8條數據:
/// <code>
/// <![cdata[
/// ...
/// <webdiyer:aspnetpager id="pager" runat="server" pagesize=8 onpagechanged="changepage"/>
/// ...
/// ]]></code></example>
[browsable(true),
description("每頁顯示的記錄數"),
category("分頁"),
defaultvalue(10)]
public int pagesize
{
get
{
object obj=viewstate["pagesize"];
return (obj==null)?10:(int)obj;
}
set
{
viewstate["pagesize"]=value;
}
}
/// <summary>
/// 獲取在當前頁之後還未顯示的剩餘記錄的項數。
/// </summary>
[browsable(false),
designerserializationvisibility(designerserializationvisibility.hidden)]
public int recordsremain
{
get
{
if(currentpageindex<pagecount)
return recordcount-(currentpageindex*pagesize);
return 0;}
}
/// <summary>
/// 獲取所有要分頁的記錄需要的總頁數。
/// </summary>
[browsable(false),
designerserializationvisibility(designerserializationvisibility.hidden)]
public int pagecount
{
get{return (int)math.ceiling((double)recordcount/(double)pagesize);}
}
#endregion
#region textbox and submit button
/// <summary>
/// 獲取或設置頁索引文本框的顯示方式。
/// </summary>
/// <remarks>
/// 頁索引文件框允許用戶手式輸入要訪問的頁的索引,當頁數非常多時,顯示頁索引文本框非常方便用戶跳轉到指定的頁,默認情況下,該文本框只有在總頁數大於或等於<see cref="showboxthreshold"/> 的值時才顯示,否則不顯示,要想該文本框任何時候都顯示,請將其值設為always,若希望任何時候都不顯示,則應設為never。
///</remarks>
[browsable(true),
description("指定頁索引文本框的顯示方式"),
category("文本框及提交按鈕"),
defaultvalue(showinputbox.auto)]
public showinputbox showinputbox
{
get
{
object obj=viewstate["showinputbox"];
return (obj==null)?showinputbox.auto:(showinputbox)obj;
}
set{viewstate["showinputbox"]=value;}
}
/// <summary>
/// 獲取或設置應用於頁索引輸入文本框的css類名。
/// </summary>
[browsable(true),
category("文本框及提交按鈕"),
defaultvalue(null),
description("應用於頁索引輸入文本框的css類名")]
public string inputboxclass
{
get
{
return (string)viewstate["inputboxclass"];
}
set
{
if(value.trim().length>0)
viewstate["inputboxclass"]=value;
}
}
/// <summary>
/// 獲取或設置頁索引輸入文本框的css樣式文本。
/// </summary>
[browsable(true),
category("文本框及提交按鈕"),
defaultvalue(null),
description("應用於頁索引輸入文本框的css樣式文本")]
public string inputboxstyle
{
get
{
return (string)viewstate["inputboxstyle"];
}
set
{
if(value.trim().length>0)
viewstate["inputboxstyle"]=value;
}
}
/// <summary>
/// 獲取或設置頁索引頁索引輸入文本框前的文本字符串值。
/// </summary>
[browsable(true),
category("文本框及提交按鈕"),
defaultvalue(null),
description("頁索引輸入文本框前的文本內容字符串")]
public string textbeforeinputbox
{
get
{
return (string)viewstate["textbeforeinputbox"];
}
set
{
viewstate["textbeforeinputbox"]=value;
}
}
/// <summary>
/// 獲取或設置頁索引文本輸入框後的文本內容字符串值。
/// </summary>
[browsable(true),
defaultvalue(null),
category("文本框及提交按鈕"),
description("頁索引輸入文本框後的文本內容字符串")]
public string textafterinputbox
{
get
{
return (string)viewstate["textafterinputbox"];
}
set
{
viewstate["textafterinputbox"]=value;
}
}
/// <summary>
/// 獲取或設置提交按鈕上的文本。
/// </summary>
[browsable(true),
category("文本框及提交按鈕"),
defaultvalue("go"),
description("提交按鈕上的文本")]
public string submitbuttontext
{
get
{
object obj=viewstate["submitbuttontext"];
return (obj==null)?"go":(string)obj;
}
set
{
if(value.trim().length>0)
viewstate["submitbuttontext"]=value;
}
}
/// <summary>
/// 獲取或設置應用於提交按鈕的css類名。
/// </summary>
[browsable(true),
category("文本框及提交按鈕"),
defaultvalue(null),
description("應用於提交按鈕的css類名")]
public string submitbuttonclass
{
get
{
return (string)viewstate["submitbuttonclass"];
}
set
{
viewstate["submitbuttonclass"]=value;
}
}
/// <summary>
/// 獲取或設置應用於提交按鈕的css樣式。
/// </summary>
[browsable(true),
category("文本框及提交按鈕"),
defaultvalue(null),
description("應用於提交按鈕的css樣式")]
public string submitbuttonstyle
{
get
{
return (string)viewstate["submitbuttonstyle"];
}
set
{
viewstate["submitbuttonstyle"]=value;
}
}
/// <summary>
/// 獲取或設置自動顯示頁索引輸入文本框的最低起始頁數。
/// </summary>
/// <remarks>
/// 當<see cref="showinputbox"/> 設為auto(默認)並且要分頁的數據的總頁數達到該值時會自動顯示頁索引輸入文本框,默認值為30。該選項當<see cref="showinputbox"/> 設為never或always時沒有任何作用。
/// </remarks>
[browsable(true),
description("指定當showinputbox設為showinputbox.auto時,當總頁數達到多少時才顯示頁索引輸入文本框"),
category("文本框及提交按鈕"),
defaultvalue(30)]
public int showboxthreshold
{
get
{
object obj=viewstate["showboxthreshold"];
return (obj==null)?30:(int)obj;
}
set{viewstate["showboxthreshold"]=value;}
}
#endregion
#region custominfosection
/// <summary>
/// 獲取或設置顯示用戶自定義信息區的方式。
/// </summary>
/// <remarks>
/// 該屬性值設為left或right時會在分頁導航元素左邊或右邊劃出一個專門的區域來顯示有關用戶自定義信息,設為never時不顯示。
/// </remarks>
[browsable(true),
description("顯示當前頁和總頁數信息,默認值為不顯示,值為showcustominfosection.left時將顯示在頁索引前,為showcustominfosection.right時將顯示在頁索引後"),
defaultvalue(showcustominfosection.never),
category("自定義信息區")]
public showcustominfosection showcustominfosection
{
get
{
object obj=viewstate["showcustominfosection"];
return (obj==null)?showcustominfosection.never:(showcustominfosection)obj;
}
set{viewstate["showcustominfosection"]=value;}
}
/// <summary>
/// 獲取或設置用戶自定義信息區文本的對齊方式。
/// </summary>
[browsable(true),
category("自定義信息區"),
defaultvalue(horizontalalign.left),
description("用戶自定義信息區文本的對齊方式")]
public horizontalalign custominfotextalign
{
get
{
object obj=viewstate["custominfotextalign"];
return (obj==null)?horizontalalign.left:(horizontalalign)obj;
}
set
{
viewstate["custominfotextalign"]=value;
}
}
/// <summary>
/// 獲取或設置用戶自定義信息區的寬度。
/// </summary>
[browsable(true),
category("自定義信息區"),
defaultvalue(typeof(unit),"40%"),
description("用戶自定義信息區的寬度")]
public unit custominfosectionwidth
{
get
{
object obj=viewstate["custominfosectionwidth"];
return (obj==null)?unit.percentage(40):(unit)obj;
}
set
{
viewstate["custominfosectionwidth"]=value;
}
}
/// <summary>
/// 獲取或設置應用於用戶自定義信息區的級聯樣式表類名。
/// </summary>
[browsable(true),
category("自定義信息區"),
defaultvalue(null),
description("應用於用戶自定義信息區的級聯樣式表類名")]
public string custominfoclass
{
get
{
object obj=viewstate["custominfoclass"];
return (obj==null)?cssclass:(string)obj;
}
set
{
viewstate["custominfoclass"]=value;
}
}
/// <summary>
/// 獲取或設置應用於用戶自定義信息區的css樣式文本。
/// </summary>
/// <value>字符串值,要應用於用戶自定義信息區的css樣式文本。 </value>
[browsable(true),
category("自定義信息區"),
defaultvalue(null),
description("應用於用戶自定義信息區的css樣式文本")]
public string custominfostyle
{
get
{
object obj=viewstate["custominfostyle"];
return (obj==null)?getstylestring():(string)obj;
}
set
{
viewstate["custominfostyle"]=value;
}
}
/// <summary>
/// 獲取或設置在顯示在用戶自定義信息區的用戶自定義文本。
/// </summary>
[browsable(true),
category("自定義信息區"),
defaultvalue(null),
description("要顯示在用戶自定義信息區的用戶自定義信息文本")]
public string custominfotext
{
get
{
return (string)viewstate["custominfotext"];
}
set
{
viewstate["custominfotext"]=value;
}
}
#endregion
#region others
/// <summary>
/// 獲取或設置一個值,該值指定是否總是顯示aspnetpager分頁按件,即使要分頁的數據只有一頁。
/// </summary>
/// <remarks>
/// 默認情況下,當要分頁的數據小於兩頁時,aspnetpager不會在頁面上顯示任何內容,將此屬性值設為true時,即使總頁數只有一頁,aspnetpager也將顯示分頁導航元素。
/// </remarks>
[browsable(true),
category("behavior"),
defaultvalue(false),
description("總是顯示分頁控件,即使要分頁的數據只要一頁")]
public bool alwaysshow
{
get
{
object obj=viewstate["alwaysshow"];
return (obj==null)?false:(bool)obj;
}
set
{
viewstate["alwaysshow"]=value;
}
}
/// <summary>
/// 獲取或設置由aspnetpager 服務器控件在客戶端呈現的級聯樣式表(css) 類。
/// </summary>
[browsable(true),
description("應用於控件的css類名"),
category("appearance"),
defaultvalue(null)]
public override string cssclass
{
get{return base.cssclass;}
set
{
base.cssclass=value;
cssclassname=value;
}
}
/// <summary>
/// 獲取或設置一個值,該值指示aspnetpager 服務器控件是否向發出請求的客戶端保持自己的視圖狀態,該屬性經重寫後不允許設為false。
/// </summary>
/// <remarks><see cref="aspnetpager"/> 服務器控件將一些重要的分頁信息保存在viewstate中,當使用url分頁方式時,雖然視圖狀態在分頁過程中沒有任何作用,但若當前頁需要回發,則必須啟用視圖狀態以便分頁控件能在頁面回發後獲取回發前的分頁狀態;當通過頁面回發(postback)的方式來分頁時,要使aspnetpager正常工作,必須啟用視圖狀態。
/// <p><note>該屬性並不能禁止用戶用<![cdata[<%@page enableviewstate=false%> ]]>頁指令來禁用整個頁面的視圖狀態,當使用此指令並且設置aspnetpager通過頁面回發來分頁時,aspnetpager因為無法獲取保存的信息而不能正常工作。 </note></p></remarks>
[browsable(false),
description("是否啟用控件的視圖狀態,該屬性的值必須為true,不允許用戶設置。"),
defaultvalue(true),
category("behavior")]
public override bool enableviewstate
{
get
{
return base.enableviewstate;
}
set
{
base.enableviewstate=true;
}
}
/// <summary>
/// 獲取或設置當用戶輸入的頁索引超出範圍(大於最大頁索引或小於最小頁索引)時在客戶端顯示的錯誤信息。
/// </summary>
[browsable(true),
description("當用戶輸入的頁索引超出範圍(大於最大頁索引或小於最小頁索引)時在客戶端顯示的錯誤信息。"),
defaultvalue("頁數超出範圍!"),
category("data")]
public string pageindexoutofrangeerrorstring
{
get
{
object obj=viewstate["pageindexoutofrangeerrorstring"];
return (obj==null)?"頁數超出範圍!":(string)obj;
}
set
{
viewstate["pageindexoutofrangeerrorstring"]=value;
}
}
/// <summary>
/// 獲取或設置當用戶輸入無效的頁索引(負值或非數字)時在客戶端顯示的錯誤信息。
/// </summary>
[browsable(true),
description("當用戶輸入無效的頁索引(負值或非數字)時在客戶端顯示的錯誤信息。"),
defaultvalue("頁索引無效!"),
category("data")]
public string invalidpageindexerrorstring
{
get
{
object obj=viewstate["invalidpageindexerrorstring"];
return (obj==null)?"頁索引無效!":(string)obj;
}
set
{
viewstate["invalidpageindexerrorstring"]=value;
}
}
#endregion
#endregion
#region control rendering logic
/// <summary>
/// 重寫<see cref="system.web.ui.control.onload"/> 方法。
/// </summary>
/// <param name="e">包含事件數據的<see cref="eventargs"/> 對象。 </param>
protected override void onload(eventargs e)
{
if(urlpaging)
{
currenturl=page.request.path;
urlparams=page.request.querystring;
string pageindex=page.request.querystring[urlpageindexname];
int index=1;
try
{
index=int.parse(pageindex);
}
catch{}
onpagechanged(new pagechangedeventargs(index));
}
else
{
inputpageindex=page.request.form[this.uniqueid+"_input"];
}
base.onload(e);
}
/// <summary>
/// 重寫<see cref="system.web.ui.control.onprerender"/>方法。
/// </summary>
/// <param name="e">包含事件數據的<see cref="eventargs"/> 對象。 </param>
protected override void onprerender(eventargs e)
{
if(pagecount>1)
{
string checkscript="<script language="javascript">function docheck(el){var r=new regexp("^s*(d+)s*$");if(r.test(el.value)){if (regexp.$1<1||regexp.$1>"+pagecount.tostring()+"){alert(""+pageindexoutofrangeerrorstring+"");document.all['"+this.uniqueid+"_input'].select( );return false;}return true;}alert(""+invalidpageindexerrorstring+"");document.all['"+this.uniqueid+"_input'].select();return false;}</script>";
if((showinputbox==showinputbox.always)||(showinputbox==showinputbox.auto&&pagecount>=showboxthreshold))
{
if(!page.isclientscriptblockregistered("checkinput"))
page.registerclientscriptblock("checkinput",checkscript);
string script="<script language="javascript" > <!-- nfunction buildurlstring(key,value){ var _key=key.tolowercase(); var prms=location.search; if(prms.length==0) return location.pathname+"?"+_key+"="+value; var params=prms.substring(1).split("&"); var newparam=""; var found=false; for(i=0;i< params.length;i++){ if(params[i].split("=")[0].tolowercase()==_key){ params[i]=_key+"="+value; found=true; break; } } if(found) return location.pathname+"?"+params.join("&"); else return location+"&"+_key+"="+value; }n//--> </script>";
if(!page.isclientscriptblockregistered("buildurlscript"))
page.registerclientscriptblock("buildurlscript",script);
}
}
base.onprerender(e);
}
/// <summary>
/// 重寫<see cref="system.web.ui.webcontrols.webcontrol.addattributestorender"/> 方法,將需要呈現的html 屬性和样式添加到指定的<see cref="system.web.ui.htmltextwriter "/> 中
/// </summary>
/// <param name="writer"></param>
protected override void addattributestorender(htmltextwriter writer)
{
if(this.page!=null)
this.page.verifyrenderinginserverform(this);
base.addattributestorender(writer);
}
///<summary>
///重寫<see cref="system.web.ui.webcontrols.webcontrol.renderbegintag"/> 方法,將<see cref="aspnetpager"/> 控件的html 開始標記輸出到指定的<see cref=" system.web.ui.htmltextwriter"/> 編寫器中。
///</summary>
///<param name="writer"><see cref="system.web.ui.htmltextwriter"/>,表示要在客戶端呈現html 內容的輸出流。 </param>
public override void renderbegintag(htmltextwriter writer)
{
bool showpager=(pagecount>1||(pagecount<=1&&alwaysshow));
writer.writeline();
writer.write("<!------------------------------ ");
writer.write("aspnetpager v4.3 start");
writer.writeline(" ------------------------------>");
writer.write("<!-------------------- ");
writer.write("copyright:2003 webdiyer(www.webdiyer.com)");
writer.write(" ---------------------");
writer.writeline(">");
base.renderbegintag(writer);
if(!showpager)
{
writer.write("<!-----因為總頁數只有一頁,並且alwaysshow屬性設為false,aspnetpager不顯示任何內容,若要在總頁數只有一頁的情況下顯示aspnetpager,請將alwaysshow屬性設為true!");
writer.write("----->");
}
if((showcustominfosection==showcustominfosection.left||showcustominfosection==showcustominfosection.right)&&showpager)
{
writer.addattribute(htmltextwriterattribute.width,"100%");
writer.addattribute(htmltextwriterattribute.style,getstylestring());
if(height!=unit.empty)
writer.addstyleattribute(htmltextwriterstyle.height,height.tostring());
writer.addattribute(htmltextwriterattribute.border,"0");
writer.addattribute(htmltextwriterattribute.cellpadding,"0");
writer.addattribute(htmltextwriterattribute.cellspacing,"0");
writer.renderbegintag(htmltextwritertag.table);
writer.renderbegintag(htmltextwritertag.tr);
writecellattributes(writer,true);
writer.renderbegintag(htmltextwritertag.td);
}
}
///<summary>
///重寫<see cref="system.web.ui.webcontrols.webcontrol.renderendtag"/> 方法,將<see cref="aspnetpager"/> 控件的html 結束標記輸出到指定的<see cref=" system.web.ui.htmltextwriter"/> 編寫器中。
///</summary>
///<param name="writer"><see cref="system.web.ui.htmltextwriter"/>,表示要在客戶端呈現html 內容的輸出流。 </param>
public override void renderendtag(htmltextwriter writer)
{
if((showcustominfosection==showcustominfosection.left||showcustominfosection==showcustominfosection.right)&&(pagecount>1||(pagecount<=1&&alwaysshow)))
{
writer.renderendtag();
writer.renderendtag();
writer.renderendtag();
}
base.renderendtag(writer);
writer.writeline();
writer.write("<!------------------------------- ");
writer.write("aspnetpager v4.3 end");
writer.write(" --------------------------------");
writer.writeline(">");
writer.writeline();
}
/// <summary>
/// 重寫<see cref="system.web.ui.webcontrols.webcontrol.rendercontents"/> 方法,將控件的內容呈現到指定<see cref="system.web.ui.htmltextwriter"/> 的編寫器中。
/// </summary>
/// <param name="writer"><see cref="system.web.ui.htmltextwriter"/>,表示要在客戶端呈現html 內容的輸出流。 </param>
protected override void rendercontents(htmltextwriter writer)
{
if(pagecount<=1&&!alwaysshow)
return;
if(showcustominfosection==showcustominfosection.left)
{
writer.write(custominfotext);
writer.renderendtag();
writecellattributes(writer,false);
writer.addattribute(htmltextwriterattribute.class,cssclass);
writer.renderbegintag(htmltextwritertag.td);
}
int midpage=(int)((currentpageindex-1)/numericbuttoncount);
int pageoffset=midpage*numericbuttoncount;
int endpage=((pageoffset+numericbuttoncount)>pagecount)?pagecount:(pageoffset+numericbuttoncount);
this.createnavigationbutton(writer,"first");
this.createnavigationbutton(writer,"prev");
if(showpageindex)
{
if(currentpageindex>numericbuttoncount)
createmorebutton(writer,pageoffset);
for(int i=pageoffset+1;i<=endpage;i++)
{
createnumericbutton(writer,i);
}
if(pagecount>numericbuttoncount&&endpage<pagecount)
createmorebutton(writer,endpage+1);
}
this.createnavigationbutton(writer,"next");
this.createnavigationbutton(writer,"last");
if((showinputbox==showinputbox.always)||(showinputbox==showinputbox.auto&&pagecount>=showboxthreshold))
{
writer.write(" ");
if(textbeforeinputbox!=null)
writer.write(textbeforeinputbox);
writer.addattribute(htmltextwriterattribute.type,"text");
writer.addstyleattribute(htmltextwriterstyle.width,"30px");
writer.addattribute(htmltextwriterattribute.value,currentpageindex.tostring());
if(inputboxstyle!=null&&inputboxstyle.trim().length>0)
writer.addattribute(htmltextwriterattribute.style,inputboxstyle);
if(inputboxclass!=null&&inputboxclass.trim().length>0)
writer.addattribute(htmltextwriterattribute.class,inputboxclass);
if(pagecount<=1&&alwaysshow)
writer.addattribute(htmltextwriterattribute.readonly,"true");
writer.addattribute(htmltextwriterattribute.name,this.uniqueid+"_input");
string scriptref="docheck(document.all['"+this.uniqueid+"_input'])";
string postref="if(event.keycode==13){if("+scriptref+")__dopostback('"+this.uniqueid+"',document.all['"+this.uniqueid+"_input'].value); else{event.returnvalue=false;}}";
string keydownscript="if(event.keycode==13){if("+scriptref+"){even
csover 發表於:2006.07.21 08:46 ::分類: ( asp.net ) ::閱讀:(464次) :: 評論(0) :: 引用(0)
2006 年07 月20日, 星期四
一個簡單的分頁控件
採用datagrid的默任的分頁方式分頁,後來發現對於大的數據量速度很慢,net進程佔用系統資源也很大,後來寫了個分頁的存儲過程,每次取數據都只取當前頁的,分頁是分好了,但是發現翻頁就沒那麼方便了,於是自己寫了個簡單的分頁控件,代碼如下(編譯以後直接形成dll就可以用)。
sample code:
using system;
using system.web.ui;
using system.web.ui.webcontrols;
using system.componentmodel;
namespace pageinfocontrol
{
///<summary>
///webcustomecontrol1的摘要說明
///</summary>
[defaultproperty('totalrecord'),toolboxdata('<{0}:pageinfo runat=server></{0}:pageinfo>')]
public class pageinfo:system.web.ui.webcontrols.webcontrol,ipostbackeventhandler
{
#region construct method
///<summary>
///構造函數
///</summary>
public pageinfo():base(htmltextwritertag.div)
{
}
#endregion
#region variables and constants
public event eventhandler changepageclick;
private string _barbackgroudcolor='#f1f1f1';
private string _barlinkcolor='navy';
private string _barcurrentcolor='#eeeeee';
private int _totalrecord=0;
private int _totalpage=0;
private int _currentpageindex=1;
private int _itemsize=10;
#endregion
#region properties
[description('分頁條背景色'),bindable(true),category('appearance'),defaultvalue('#f1f1f1')]
public string barbackgroundcolor
{
get{return _barbackgroundcolor;}
set{_barbackgroundcolor=value;}
}
[description('分頁條帶鏈接數字顏色'),bindable(true),category('appearance'),defaultvalue('navy')]
public string barlinkcolor
{
get{return _barlinkcolor;}
set{_barlinkcolor=value;}
}[description('分頁條當前頁數字顏色'),bindable(true),category('appearance'),defaultvalue('#eeeeee')]
public string barcurrentcolor
{
get{return _barcurrentcolor;}
set{_barcurrentcolor=value;}
} [description('總記錄數'),bindable(false),category('behavior'),defaultvalue(0)]
public int totalrecord
{
get{return _totalrecord;}
set
{
foreach(char c in system.convert.tostring(value))
{
if(!char.isnumber(c)
{
_totalrecord=0;
break;
}
_totalrecord=value;
}
} [description('每頁顯示記錄數'),bindable(true),category('behavior'),defaultvalue(0)]
public int pagesize
{
get{return _pagesize;}
set
{
foreach(char c in system.convert.tostring(value))
{
if(!char.isnumber(c))
{
_pagesize=0;
break;
}
}
_pagesize=value;
}
}[description('總頁數'),bindable(true),category('behavior'),defaultvalue(0)]
public int totalpage
{
get{return _totalpage;}
}[description('數字規格'),bindable(true),category('behavior'),defaultvalue(10)]
public int itemsize
{
get{return _itemsize;}
set
{
foreach(char c in system.convert.tostring(value))
{
if(!char.isnumber(c))
{
_itemsize=10;
break;
}
}
_itemsize=value;
}
}[description('當前頁值'),bindable(true),category('behavior'),defaultvalue(1)]
public int currentpageindex
{
get{return _currentpageindex;}
set{_currentpageindex=value;}
}
#endregion
//定義div的樣式
protected override void addattributestorender(htmltextwriter writer)
{
writer.addstyleattribute('white-space','nowrap');
writer.addstyleattribute('padding-top','2px');
writer.addstyleattribute('padding-bottom',2px');
writer.addstyleattribute('width',width.tostring());
writer.addstyleattribute('height',height.tostring());
base.addattributestorender(writer);
}
protected virtual void onpagechangeclick(eventargs e)
{
if(changepageclick!=null)
{
changepageclick(this,e);
}
} public void raisepostbackevent(string eventargument)
{
int pageindex=int.parse(eventargument);
this._currentpageindex=pageindex;
onpagechangeclick(new eventargs());
}
///<summary>
///將此控件呈現給指定的輸出參數
///</summary>
///<param name='output'>要寫出到的html編寫器</param>
protected override void rendercontents(htmltextwriter output)
{this._totalpage=((this.totalrecord/pagesize)*this.pagesize==this.totalrecord)?(this.totalrecord/this.pagesize):((this.totalrecord/this.pagesize)+1);
int beginrecord=(this.currentpageindex-1)*this.pagesize+1;
int endrecord=this.currentpageindex*this.pagesize;
string pageinfo='[共<font color=#cc0000>'+this.totalpage.tostring()+'</font>頁/當前第<font color=#cc0000>'+this.currentpageindex.tostring()+' </font>頁共<font color=#cc0000>'+totalrecord.tostring()+'</font>條記錄,當前記錄數<font color=#cc0000>'+begingrecord.tostring()+'</ font>到<font color=#cc0000>'+endrecord.tostring()+'</font>]';
string pageliststr='';
string pageindexcolor='#0000c0';
int singlenumber=this.totalpage-(totalpage/itemsize)*itemsize; //得到分頁後的尾數(比如:總共58頁,按10頁規格顯示,則尾數為8)
int intpageformax=(this.currentpageindex-1)/itemsize;
int minint=(1+itemsize*intpageformax);
int maxint=((intpageformax+1)*itemsize)>totalpage?totalpage:((intpageformax+1)*itemsize);
if(this.totalrecord==0||this.totalpage==0)
{
pageliststr='<font color='+pageindexcolor+'>0</font>';
pageliststr=pageliststr+'[共<font color=#cc0000>0</font>頁/當前第<font color=#cc0000>0</font>頁共<font color=#cc0000>0</font>條記錄,當前記錄數<font color=#cc0000>0</font>到<font color=#cc0000>0</font>]';
output.write(pageliststr);
}
else
{
if(this.totalpage<=this.itemsize)
{
for(int i=1;i<=totalpage;i++)
{
pageindexcolor=currentpageindex==i?'#cc0000':'#0000c0';
if(currentpageindex==i)
pageliststr=pageliststr+'<a title='當前為第['+i+']頁' href='#' id=''+this.uniqueid+''><font color='+pageindexcolor+'>'+i.tostring ()+'</font></a>';
else
pageliststr=pageliststr+'<a title='點擊轉到第['+i+']頁' id=''+this.uniqueid+'' href=' javascript:'+page.getpostbackeventreference(this,i.tostring()) +''><font color='+pageindexcolor+'>'+i.tostring()+'</font></a>';
}
pageliststr=pageliststr==''?'<font color='+pageindexcolor+'>0</font>':pageliststr;
pageliststr=pageliststr+''+pageinfo;
output.write(pageliststr);
}
else
{
for(int i=minint;i<=maxint;i++)
{
pageindexcolor=currentpageindex==i?'#cc0000':'#0000c0';
if(currentpageindex==i)
pageliststr=pageliststr+'<a title='當前為第['+i+']頁' href='#' id=''+this.uniqueid+''><font color='+pageindexcolor+'>'+i.tostring ()+'</font></a>';
else
pageliststr=pageliststr+'<a title='點擊轉到第['+i+']頁' id=''+this.uniqueid+'' href=' javascript:'+page.getpostbackeventreference(this,i.tostring()) +''><font color='+pageindexcolor+'>'+i.tostring()+'</font></a>';
}
//噹噹前頁數小於itemsize且總的頁數大於itemsize時
if(currentpageindex<=itemsize && totalpage>itemsize)
{
pageliststr=pageliststr+'<a id=''+this.uniqueid+'' title='點擊轉到第['+system.convert.tostring(itemsize+1)+']頁' href=' javascript:'+page. getpostbackeventreference(this.system.convert.tostring(itemsize+1))+''>>></a>';
//噹噹前頁數大於itemsize,且總的頁數減去當前頁數大於等於尾數值頁數時
if(this.currentpageindex>itemsize && (totalpage-this.currentpageindex)>=singlenumber)
{
int multiminpageindex=(intpageformax*itemsize);
int multimaxpageindex=((intpageformax+1)*itemsize)+1;
pageliststr='<a id=''+this.uniqueid+'' title='點擊轉到第['+multiminpageindex+']頁' href=' javascrcipt:'+page.getpostbackeventreference(this.multiminpageindex.tostring())+ ''><<</a>'+pageliststr.trim()+'<a id=''+this.uniqueid+'' title='點擊轉到第['+multimaxpageindex+']頁' href=' javascript: '+page.getpostbackeventreference(this.multimaxpageindex.tostring())+''>>></a>';
}
//噹噹前頁數大於itemsize,且總的頁數減去當前頁數大於等於尾數值頁數時
if(currentpageindex>10 && (totalpage-currentpageindex)<singlenumber)
{
int multiminpageindex=(intpageformax * itemsize);
pageliststr='<a id=''+this.uniqueid+'' title='點擊轉到第['+multiminpageindex+']頁' href=' javascript:'+page.getpostbackeventreference(this,multiminpageindex.tostring ())+ ''><<</a>'+pageliststr.trim();
}
pageliststr=pageliststr==''?'<font color='+pageindexcolor+'>0</font>':pageliststr;
pageliststr=pageliststr+''+pageinfo;
output.write(pageliststr);
}
}
base.rendercontents(output);
}
}
}
控件中有幾個相關的屬性,在使用的時候,只需要指定:totalrecord(總記錄數)、pagesize(每頁的數據記錄數)、currentpageindex(當前頁面值)、itemsize(分頁條顯示頁面值的規格)
控件中有個changepageclick事件,可以利用“控件id.currentpageindex”屬性來獲取當前頁面值。
註冊會員,創建你的web開發資料庫,