asp.net 無(wú)刷新翻頁(yè)就是這么簡(jiǎn)單
一步一步看來(lái):
代碼
[DefaultProperty("TotalRecord"),
ToolboxData("<{0}:AjaxPager runat=server></{0}:AjaxPager>")]
public class AjaxPager : WebControl,ICallbackEventHandler
{
public AjaxPager()
: base(HtmlTextWriterTag.Div)
{
this.Load += new EventHandler(AjaxPager_Load);
}
void AjaxPager_Load(object sender, EventArgs e)
{
string script = "function AjaxPagerCallBack(returnData){var parts =returnData.split('[_]'); document.getElementById('" + this.UniqueID.Replace ('$','_') + "').innerHTML = parts[0];document.getElementById('" + Info.ContainID + "').innerHTML=parts[1]}";
this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "AjaxPagerCallBack", script, true);
}
}
這里在Load事件里向頁(yè)面注冊(cè)了一段JS,AjaxPagerCallBack方法做兩個(gè)操作,把自己表現(xiàn)的數(shù)據(jù)放入自己在客戶端生成的DIV容器里,即id為this.UniqueID.Replace('$','_')的div,ClientID好像也可以!哈,一時(shí)糊涂!第二步就是把分頁(yè)數(shù)據(jù)放到id為Info.ContainID的div中,Info對(duì)象下面會(huì)說(shuō)到。
變量屬性
#region 變量
private string _BarBackGroundColor = "#FFFFFF";
private string _BarLinkColor = "Navy";
private string _BarCurrentColor = "#EEEEEE";
private int _TotalRecord = 50;
private int _TotalPage = 0;
private int _CurrentIndex = 1;
private int _ItemSize = 10;
private PageInfo _Info=new PageInfo ();
#endregion
#region 屬性
#region 外觀
[Description("分頁(yè)背景色"),
Bindable(true),
Category("外觀"),
DefaultValue("#FFFFFF")]
public string BarBackGroundColor
{
get { return _BarBackGroundColor; }
set { _BarBackGroundColor = value; }
}
[Description("分頁(yè)條鏈接數(shù)字顏色"),
Bindable(true),
Category("外觀"),
DefaultValue("Navy")]
public string BarLinkColor
{
get { return _BarLinkColor; }
set { _BarLinkColor = value; }
}
[Description("分頁(yè)條當(dāng)前頁(yè)數(shù)字顏色"),
Bindable(true),
Category("外觀"),
DefaultValue("#EEEEEE")]
public string BarCurrentColor
{
get { return _BarCurrentColor; }
set { _BarCurrentColor = value; }
}
#endregion
#region 行為
[Description("總記錄數(shù)"),
Category("行為"),
DefaultValue(50)]
public int TotalRecord
{
get { return _TotalRecord; }
set
{
_TotalRecord = value;
}
}
[Description("總頁(yè)數(shù)"),
Category("行為"),
DefaultValue(0)]
public int TotalPage
{
get { return _TotalPage; }
}
[Description("Bar的大小"),
Category("行為"),
DefaultValue(10)]
public int BarSize
{
get { return _ItemSize; }
set
{
foreach (char c in System.Convert.ToString(value))
{
if (!Char.IsNumber(c))
{
_ItemSize = 10;
break;
}
}
_ItemSize = value;
}
}
[Description("當(dāng)前頁(yè)值"),
Category("行為"),
DefaultValue(1)]
public int PageIndex
{
get { return _CurrentIndex; }
set { _CurrentIndex = value; }
}
#endregion
public PageInfo Info
{
get { return _Info; }
set { _Info = value; }
}
#endregion
這里自不必細(xì)說(shuō)。其中的PageInfo如下:
PageInfo
[Serializable]
public class PageInfo
{
private string _RepeaterUniqueID;
private string _ContainID="AjaxData";
private string _TableName = string.Empty;
private string _IdentityField = "ID";
private int _PageSize = 10;
private string _Fields = "*";
private bool _IsDesc = true;
private string _Content = string.Empty;
private string _ConnectStringName = string.Empty;
public string RepeaterUniqueID
{
get { return _RepeaterUniqueID; }
set { _RepeaterUniqueID = value; }
}
public string ContainID
{
get { return _ContainID; }
set { _ContainID = value; }
}
public int PageSize
{
get { return _PageSize; }
set
{
_PageSize = int.Parse(value.ToString());
}
}
public string TableName
{
get { return _TableName; }
set { _TableName = value; }
}
public string IdentityField
{
get { return _IdentityField; }
set { _IdentityField = value; }
}
public string Fields
{
get { return _Fields; }
set { _Fields = value; }
}
public bool IsDesc
{
get { return _IsDesc; }
set { _IsDesc = value; }
}
public string Content
{
get { return _Content; }
set { _Content = value; }
}
public string ConnectStringName
{
get { return _ConnectStringName; }
set { _ConnectStringName = value; }
}
}
這是標(biāo)記為Serializable,是因?yàn)橄旅嬉4娴絍iewState里。
輔助方法
private string GetContents()
{
this._TotalPage = ((this.TotalRecord / this.Info.PageSize) * this.Info.PageSize == this.TotalRecord) ? (this.TotalRecord / this.Info.PageSize) : ((this.TotalRecord / this.Info.PageSize) + 1);
int BeginRecord = (this.PageIndex - 1) * this.Info.PageSize + 1;
int EndRecord = Math.Min(this.PageIndex * this.Info.PageSize, this.TotalRecord);
string PageInfo = string.Format("[每頁(yè)<span style='color:#CC0000'>{0}({1}-{2})</span>條 共<span style='color:#CC0000'>{3}</span>條<span style='color:#CC0000'>{4}</span>頁(yè)]", Info.PageSize, BeginRecord, EndRecord, this.TotalRecord, this.TotalPage);
StringBuilder PageListStr = new StringBuilder();
string PageIndexColor = "#000000";
int SingleNumber = this.TotalPage - (TotalPage / BarSize) * BarSize;
int IntPageForMax = (this.PageIndex - 1) / BarSize;
int MinInt = (1 + BarSize * IntPageForMax);
int MaxInt = ((IntPageForMax + 1) * BarSize) > TotalPage ? TotalPage : ((IntPageForMax + 1) * BarSize);
if (this.TotalRecord == 0 || this.TotalPage == 0)
{
PageListStr.AppendFormat("<span style='color:'{0};margin:auto 3px;'>0</span>", PageIndexColor);
PageListStr.AppendFormat(" [共<span style='color:#CC0000'>0</span>頁(yè)/當(dāng)前第<span style='color:#CC0000'>0</span>頁(yè) 共<span style='color:#CC0000'>0</span>條記錄,當(dāng)前記錄數(shù)<span style='color:#CC0000'>0</span>到<span style='color:#CC0000'>0</span>]");
return PageListStr.ToString();
}
else
{
if (this.TotalPage <= this.BarSize)
{
for (int i = 1; i <= TotalPage; i++)
{
PageIndexColor = PageIndex == i ? "#CC0000" : "#000000";
if (PageIndex == i)
PageListStr.AppendFormat("<a id='{0}' style='color:{1};margin:auto 3px;'>{2}</a>", this.UniqueID, PageIndexColor, i);
else
PageListStr.AppendFormat("<a id='{0}' style='color:{1};margin:auto 3px;' href=\"javascript:{2}\">{3}</a>", this.UniqueID, PageIndexColor, Page.ClientScript.GetCallbackEventReference(this, i.ToString(),"AjaxPagerCallBack",null), i);
}
PageListStr.AppendFormat(" {0}", PageInfo);
return PageListStr.ToString();
}
else
{
for (int i = MinInt; i <= MaxInt; i++)
{
PageIndexColor = PageIndex == i ? "#CC0000" : "#000000";
if (PageIndex == i)
PageListStr.AppendFormat("<a id={0}' style='color:{1};margin:auto 3px;'>{2}</a>", this.UniqueID, PageIndexColor, i);
else
PageListStr.AppendFormat("<a id='{0}' style='color:{1};margin:auto 3px;' href=\"javascript:{2}\">{3}</a>", this.UniqueID, PageIndexColor, Page.ClientScript.GetCallbackEventReference(this, i.ToString(), "AjaxPagerCallBack", null), i);
}
if (PageIndex <= BarSize && TotalPage > BarSize)
{
PageListStr.AppendFormat("<a id='{0}' href=\"javascript:{1}\">下一頁(yè)</a>", this.UniqueID, Page.ClientScript.GetCallbackEventReference(this, System.Convert.ToString(BarSize + 1), "AjaxPagerCallBack", null));
}
if (this.PageIndex > BarSize && (TotalPage - this.PageIndex) >= SingleNumber)
{
int MultiMinPageIndex = (IntPageForMax * BarSize);
int MultiMaxPageIndex = ((IntPageForMax + 1) * BarSize) + 1;
PageListStr.Insert(0, string.Format("<a id='{0}' href=\"javascript:{1}\">上一頁(yè)</a>", this.UniqueID, Page.ClientScript.GetCallbackEventReference(this, MultiMinPageIndex.ToString(),"AjaxPagerCallBack",null)));
PageListStr.AppendFormat("<a id='{0}' href=\"javascript:{1}\">下一頁(yè)</a>", this.UniqueID, Page.ClientScript.GetCallbackEventReference(this, MultiMaxPageIndex.ToString(),"AjaxPagerCallBack",null));
}
if (PageIndex > 10 && (TotalPage - PageIndex) < SingleNumber)
{
int MultiMinPageIndex = (IntPageForMax * BarSize);
PageListStr.Insert(0, string.Format("<a id='{0}' href=\"javascript:{1}\">上一頁(yè)</a>", this.UniqueID, Page.ClientScript.GetCallbackEventReference(this, MultiMinPageIndex.ToString(), "AjaxPagerCallBack", null)));
}
PageListStr.AppendFormat(" {0}", PageInfo);
return PageListStr.ToString();
}
}
}
public void BindData()
{
Repeater rpt = getRpt();
rpt.Visible = true;
SqlHelper helper;
helper = this.Info.ConnectStringName.IsNullOrEmpty() ? new SqlHelper() : new SqlHelper(Info.ConnectStringName);
if (this.Info.RepeaterUniqueID.IsNullOrEmpty())
{
throw new Exception("必須給Info的RepeaterUniqueID屬性賦值");
}
int count = 0;
DataTable dt = helper.GetPageData(Info.TableName, Info.Fields, Info.IdentityField, Info.PageSize, PageIndex, Info.IsDesc, Info.Content, out count);
this.TotalRecord = count;
rpt.DataShow(dt);
}
private Repeater getRpt()
{
return this.Page.FindControl(this.Info.RepeaterUniqueID) as Repeater;
}
先感謝一下寫(xiě)那個(gè)Pager的人,GetContents(得到自己分頁(yè)后的HTML)里我只做了少許改動(dòng),要不然還得細(xì)細(xì)算來(lái)?。?
BindData(用到了我的SqlHelper)是利用為服務(wù)器的DataBind()方法把數(shù)據(jù)放到repeater里,只是不讓它呈示,嘿嘿!
getRpt只是找到Repeater引用
維護(hù)視圖狀態(tài)
#region 維護(hù)視圖狀態(tài)
protected override void LoadViewState(object savedState)
{
Triplet tp = savedState as Triplet;
this.TotalRecord = Convert.ToInt32(tp.Third);
this.Info = tp.Second as PageInfo;
base.LoadViewState(tp.First);
}
protected override object SaveViewState()
{
Triplet tp = new Triplet();
tp.First = base.SaveViewState();
tp.Second = Info;
tp.Third = this.TotalRecord;
return tp;
}
#endregion
這里也不必說(shuō),只是PageInfo一定要能Serializable。
重寫(xiě)方法
#region 重寫(xiě)方法
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write(GetContents());
base.RenderContents(writer);
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddStyleAttribute("White-space", "nowrap");
writer.AddStyleAttribute("padding-top", "2px");
writer.AddStyleAttribute("padding-bottom", "2px");
writer.AddStyleAttribute("color", "#949494");
writer.AddStyleAttribute("font-weight", "bold");
writer.AddStyleAttribute("background-color", this.BarBackGroundColor);
base.AddAttributesToRender(writer);
}
#endregion
也不用說(shuō),大家一看都明白。
實(shí)現(xiàn)ICallbackEventHandler
#region ICallbackEventHandler 成員
public string GetCallbackResult()
{
StringBuilder sb=new StringBuilder ();
StringWriter sw=new StringWriter (sb);
getRpt().RenderControl(new HtmlTextWriter(sw));
return this.GetContents() + "[_]" + sb.ToString();
}
public void RaiseCallbackEvent(string eventArgument)
{
int pageindex = int.Parse(eventArgument);
this._CurrentIndex = pageindex;
BindData();
}
#endregion
回調(diào)時(shí)先執(zhí)行RaiseCallbackEvent,所以CurrentIndex改變了, BindData()執(zhí)行了?。。。?
返回時(shí)時(shí)執(zhí)行GetCallbackResult,string用"[_]"分開(kāi),對(duì)應(yīng)上面注冊(cè)的AjaxPagerCallBack js方法中的var parts =returnData.split('[_]');
OK!簡(jiǎn)單的Ajax分頁(yè)就這樣簡(jiǎn)單的完成了!??!
Northwind Orders表調(diào)用如下:
頁(yè)面中Repeater包含在<div id="AjaxData"></div>中
代碼
private void BindPage(string content)
{
SinoHelper.PageInfo info = new SinoHelper.PageInfo();
info.PageSize = 5;
info.RepeaterUniqueID = rpt.UniqueID;
info.TableName = "Orders";
info.Fields = "OrderID,CustomerID,ShipCity";
info.IdentityField = "OrderID";
info.Content = content;
AjaxPager1.Info = info;
AjaxPager1.BindData();
}
附下載:
asp.net無(wú)刷新分頁(yè)
- asp.net中System.Timers.Timer的使用方法
- asp.net Timer的使用方法
- 利用Timer在ASP.NET中實(shí)現(xiàn)計(jì)劃任務(wù)的方法
- asp.net中利用Jquery+Ajax+Json實(shí)現(xiàn)無(wú)刷新分頁(yè)的實(shí)例代碼
- asp.net+jquery ajax無(wú)刷新登錄的實(shí)現(xiàn)方法
- asp.net 無(wú)刷新分頁(yè)實(shí)例代碼
- asp.net jquery無(wú)刷新分頁(yè)插件(jquery.pagination.js)
- asp.net ajax實(shí)現(xiàn)無(wú)刷新驗(yàn)證碼
- asp.net 30分鐘掌握無(wú)刷新 Repeater
- asp.net+js 實(shí)現(xiàn)無(wú)刷新上傳解析csv文件的代碼
- asp.net中Timer無(wú)刷新定時(shí)器的實(shí)現(xiàn)方法
相關(guān)文章
Asp.net中安全退出時(shí)清空Session或Cookie的實(shí)例代碼
網(wǎng)站中點(diǎn)擊退出,如果僅僅是重定向到登錄/出頁(yè)面,此時(shí)在瀏覽器地址欄中輸入登錄后的某個(gè)頁(yè)面地址如主頁(yè),你會(huì)發(fā)現(xiàn)不用登錄就能訪問(wèn),這種退出并不安全了,下面通過(guò)本文給大家介紹安全退出時(shí)清空Session或Cookie的實(shí)例代碼2016-11-11asp.net 分頁(yè)sql語(yǔ)句(結(jié)合aspnetpager)
一直用的是存儲(chǔ)過(guò)程分頁(yè),小項(xiàng)目一般不寫(xiě)存儲(chǔ)過(guò)程,就需要直接寫(xiě)分頁(yè)sql語(yǔ)句。2009-01-01淺談.Net并行計(jì)算之?dāng)?shù)據(jù)并行
這篇文章主要介紹了.Net并行計(jì)算之?dāng)?shù)據(jù)并行,有需要的朋友可以參考一下2013-12-12Asp.net?core?使用SignalR推送消息過(guò)程詳解
ASP.NET?Core?SignalR?是一個(gè)開(kāi)放源代碼庫(kù),可用于簡(jiǎn)化向應(yīng)用添加實(shí)時(shí)?Web?功能。?實(shí)時(shí)?Web?功能使服務(wù)器端代碼能夠?qū)?nèi)容推送到客戶端,本文重點(diǎn)給大家介紹Asp.net?core?使用SignalR推送消息,感興趣的朋友一起看看吧2022-03-03ASP.NET MVC5+EF6+EasyUI 后臺(tái)管理系統(tǒng)(81)-數(shù)據(jù)篩選(萬(wàn)能查詢(xún))實(shí)例
本篇文章主要介紹了ASP.NET MVC5+EF6+EasyUI 后臺(tái)管理系統(tǒng)(81)-數(shù)據(jù)篩選(萬(wàn)能查詢(xún)) ,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12在.NET?Core中使用CSRedis的詳細(xì)過(guò)程
這篇文章主要介紹了在.NET?Core中使用CSRedis的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06asp.net core服務(wù)限制堆內(nèi)存大小的操作方法
asp.net core是微軟旗下支持跨平臺(tái)的開(kāi)發(fā)框架,與springboot思想類(lèi)似,支持ioc等,可以快速的開(kāi)發(fā)web api等項(xiàng)目,這篇文章主要介紹了asp.net core服務(wù)限制堆內(nèi)存大小,需要的朋友可以參考下2022-09-09.NET Core基于Generic Host實(shí)現(xiàn)后臺(tái)任務(wù)方法教程
這篇文章主要給大家介紹了關(guān)于.NET Core基于Generic Host實(shí)現(xiàn)后臺(tái)任務(wù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11jQuery+Ajax用戶登錄功能的實(shí)現(xiàn)
前幾天把jbox源碼修改成仿QQ空間模擬窗口后發(fā)現(xiàn)有很多人在關(guān)注。今天就貼一下我利用該模擬窗口實(shí)現(xiàn)的用戶登錄功能的代碼。2009-11-11