ICallbackEventHandler实现的无刷新翻页组件

01.[DefaultProperty("TotalRecord"),

02.     ToolboxData("<{0}:AjaxPager runat=server></{0}:AjaxPager>")]
03.     public class AjaxPager : WebControl,ICallbackEventHandler
04.     {
05.         public AjaxPager()
06.             : base(HtmlTextWriterTag.Div)
07.         {
08.             this.Load += new EventHandler(AjaxPager_Load);
09.         }
10.         void AjaxPager_Load(object sender, EventArgs e)
11.         {
12.             string script = "function AjaxPagerCallBack(returnData){var parts =returnData.split('[_]'); document.getElementById('" + this.UniqueID.Replace ('$','_') + "').innerHTML = parts[0];document.getElementById('" + Info.ContainID + "').innerHTML=parts[1]}";
13.             this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "AjaxPagerCallBack", script, true);
14.         }
15. }

这里在Load事件里向页面注册了一段JS,AjaxPagerCallBack方法做两个操作,把自己表现的数据放入自己在客户端生成的DIV容器里,即id为this.UniqueID.Replace('$','_')的div,ClientID好像也可以!哈,一时糊涂!第二步就是把分页数据放到id为Info.ContainID的div中,Info对象下面会说到。

变量属性

01. #region 变量
02.         private string _BarBackGroundColor = "#FFFFFF";
03.         private string _BarLinkColor = "Navy";
04.         private string _BarCurrentColor = "#EEEEEE";
05.         private int _TotalRecord = 50;
06.         private int _TotalPage = 0;
07.         private int _CurrentIndex = 1;
08.         private int _ItemSize = 10;
09.         private PageInfo _Info=new PageInfo ();
10.           
11.         #endregion
12.         #region 属性
13.         #region 外观
14.         [Description("分页背景色"),
15.         Bindable(true),
16.         Category("外观"),
17.         DefaultValue("#FFFFFF")]
18.         public string BarBackGroundColor
19.         {
20.             get { return _BarBackGroundColor; }
21.             set { _BarBackGroundColor = value; }
22.         }
23.         [Description("分页条链接数字颜色"),
24.         Bindable(true),
25.         Category("外观"),
26.         DefaultValue("Navy")]
27.         public string BarLinkColor
28.         {
29.             get { return _BarLinkColor; }
30.             set { _BarLinkColor = value; }
31.         }
32.         [Description("分页条当前页数字颜色"),
33.         Bindable(true),
34.         Category("外观"),
35.         DefaultValue("#EEEEEE")]
36.         public string BarCurrentColor
37.         {
38.             get { return _BarCurrentColor; }
39.             set { _BarCurrentColor = value; }
40.         }
41.         #endregion
42.         #region 行为
43.         [Description("总记录数"),
44.         Category("行为"),
45.         DefaultValue(50)]
46.         public int TotalRecord
47.         {
48.             get { return _TotalRecord; }
49.             set
50.             {
51.                 _TotalRecord = value;
52.             }
53.         }
54.         [Description("总页数"),
55.         Category("行为"),
56.         DefaultValue(0)]
57.         public int TotalPage
58.         {
59.             get { return _TotalPage; }
60.         }
61.         [Description("Bar的大小"),
62.         Category("行为"),
63.         DefaultValue(10)]
64.         public int BarSize
65.         {
66.             get { return _ItemSize; }
67.             set
68.             {
69.                 foreach (char c in System.Convert.ToString(value))
70.                 {
71.                     if (!Char.IsNumber(c))
72.                     {
73.                         _ItemSize = 10;
74.                         break;
75.                     }
76.                 }
77.                 _ItemSize = value;
78.             }
79.         }
80.         [Description("当前页值"),
81.         Category("行为"),
82.         DefaultValue(1)]
83.         public int PageIndex
84.         {
85.             get { return _CurrentIndex; }
86.             set { _CurrentIndex = value; }
87.         }
88.         #endregion
89.         public PageInfo Info
90.         {
91.             get { return _Info; }
92.             set { _Info = value; }
93.         }
94.         #endregion
这里自不必细说。其中的PageInfo如下:
PageInfo
01. [Serializable]
02.     public class PageInfo
03.     {
04.         private string _RepeaterUniqueID;
05.         private string _ContainID="AjaxData";
06.         private string _TableName = string.Empty;
07.         private string _IdentityField = "ID";
08.         private int _PageSize = 10;
09.         private string _Fields = "*";
10.         private bool _IsDesc = true;
11.         private string _Content = string.Empty;
12.         private string _ConnectStringName = string.Empty;
13.         public string RepeaterUniqueID
14.         {
15.             get { return _RepeaterUniqueID; }
16.             set { _RepeaterUniqueID = value; }
17.         }
18.         public string ContainID
19.         {
20.             get { return _ContainID; }
21.             set { _ContainID = value; }
22.         }
23.         public int PageSize
24.         {
25.             get { return _PageSize; }
26.             set
27.             {
28.                 _PageSize = int.Parse(value.ToString());
29.             }
30.         }
31.         public string TableName
32.         {
33.             get { return _TableName; }
34.             set { _TableName = value; }
35.         }
36.         public string IdentityField
37.         {
38.             get { return _IdentityField; }
39.             set { _IdentityField = value; }
40.         }
41.         public string Fields
42.         {
43.             get { return _Fields; }
44.             set { _Fields = value; }
45.         }
46.         public bool IsDesc
47.         {
48.             get { return _IsDesc; }
49.             set { _IsDesc = value; }
50.         }
51.         public string Content
52.         {
53.             get { return _Content; }
54.             set { _Content = value; }
55.         }
56.         public string ConnectStringName
57.         {
58.             get { return _ConnectStringName; }
59.             set { _ConnectStringName = value; }
60.         }
61.     }
这是标记为Serializable,是因为下面要保存到ViewState里。

辅助方法
01. private string GetContents()
02.         {
03.             this._TotalPage = ((this.TotalRecord / this.Info.PageSize) * this.Info.PageSize == this.TotalRecord) ? (this.TotalRecord / this.Info.PageSize) : ((this.TotalRecord / this.Info.PageSize) + 1);
04.             int BeginRecord = (this.PageIndex - 1) * this.Info.PageSize + 1;
05.             int EndRecord = Math.Min(this.PageIndex * this.Info.PageSize, this.TotalRecord);
06.             string PageInfo = string.Format("[每页<span style='color:#CC0000'>{0}({1}-{2})</span>条&nbsp;&nbsp;共<span style='color:#CC0000'>{3}</span>条<span style='color:#CC0000'>{4}</span>页]", Info.PageSize, BeginRecord, EndRecord, this.TotalRecord, this.TotalPage);
07.             StringBuilder PageListStr = new StringBuilder();
08.             string PageIndexColor = "#000000";
09.             int SingleNumber = this.TotalPage - (TotalPage / BarSize) * BarSize;
10.             int IntPageForMax = (this.PageIndex - 1) / BarSize;
11.             int MinInt = (1 + BarSize * IntPageForMax);
12.             int MaxInt = ((IntPageForMax + 1) * BarSize) > TotalPage ? TotalPage : ((IntPageForMax + 1) * BarSize);
13.             if (this.TotalRecord == 0 || this.TotalPage == 0)
14.             {
15.                 PageListStr.AppendFormat("<span style='color:'{0};margin:auto 3px;'>0</span>", PageIndexColor);
16.                 PageListStr.AppendFormat(" [共<span style='color:#CC0000'>0</span>页/当前第<span style='color:#CC0000'>0</span>页   共<span style='color:#CC0000'>0</span>条记录,当前记录数<span style='color:#CC0000'>0</span>到<span style='color:#CC0000'>0</span>]");
17.                 return PageListStr.ToString();
18.             }
19.             else
20.             {
21.                 if (this.TotalPage <= this.BarSize)
22.                 
23.                     for (int i = 1; i <= TotalPage; i++)
24.                     {
25.                         PageIndexColor = PageIndex == i ? "#CC0000" : "#000000";
26.                         if (PageIndex == i)
27.                             PageListStr.AppendFormat("<a id='{0}' style='color:{1};margin:auto 3px;'>{2}</a>", this.UniqueID, PageIndexColor, i);
28.                         else
29.                             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);
30.                     }
31.                     PageListStr.AppendFormat(" {0}", PageInfo);
32.                     return PageListStr.ToString();
33.                 }
34.                 else
35.                 {
36.                     for (int i = MinInt; i <= MaxInt; i++)
37.                     {
38.                         PageIndexColor = PageIndex == i ? "#CC0000" : "#000000";
39.                         if (PageIndex == i)
40.                             PageListStr.AppendFormat("<a id={0}' style='color:{1};margin:auto 3px;'>{2}</a>", this.UniqueID, PageIndexColor, i);
41.                         else
42.                             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);
43.                     }
44.                     if (PageIndex <= BarSize && TotalPage > BarSize)
45.                     {
46.                         PageListStr.AppendFormat("<a id='{0}' href=/"javascript:{1}/">下一页</a>", this.UniqueID, Page.ClientScript.GetCallbackEventReference(this, System.Convert.ToString(BarSize + 1), "AjaxPagerCallBack", null));
47.                     }
48.                     if (this.PageIndex > BarSize && (TotalPage - this.PageIndex) >= SingleNumber)
49.                     {
50.                         int MultiMinPageIndex = (IntPageForMax * BarSize);
51.                         int MultiMaxPageIndex = ((IntPageForMax + 1) * BarSize) + 1;
52.                         PageListStr.Insert(0, string.Format("<a id='{0}' href=/"javascript:{1}/">上一页</a>", this.UniqueID, Page.ClientScript.GetCallbackEventReference(this, MultiMinPageIndex.ToString(),"AjaxPagerCallBack",null)));
53.                         PageListStr.AppendFormat("<a id='{0}' href=/"javascript:{1}/">下一页</a>", this.UniqueID, Page.ClientScript.GetCallbackEventReference(this, MultiMaxPageIndex.ToString(),"AjaxPagerCallBack",null));
54.                     }
55.                     if (PageIndex > 10 && (TotalPage - PageIndex) < SingleNumber)
56.                     {
57.                         int MultiMinPageIndex = (IntPageForMax * BarSize);
58.                         PageListStr.Insert(0, string.Format("<a id='{0}' href=/"javascript:{1}/">上一页</a>", this.UniqueID, Page.ClientScript.GetCallbackEventReference(this, MultiMinPageIndex.ToString(), "AjaxPagerCallBack", null)));
59.                     }
60.                     PageListStr.AppendFormat(" {0}", PageInfo);
61.                     return PageListStr.ToString();
62.                 }
63.             }
64.         }
65.         public void  BindData()
66.         {
67.             Repeater rpt = getRpt();
68.             rpt.Visible = true;
69.             SqlHelper helper;
70.             helper = this.Info.ConnectStringName.IsNullOrEmpty() ? new SqlHelper() : new SqlHelper(Info.ConnectStringName);
71.             if (this.Info.RepeaterUniqueID.IsNullOrEmpty())
72.             {
73.                 throw new Exception("必须给Info的RepeaterUniqueID属性赋值");
74.             }
75.             int count = 0;
76.             DataTable dt = helper.GetPageData(Info.TableName, Info.Fields, Info.IdentityField, Info.PageSize, PageIndex, Info.IsDesc, Info.Content, out count);
77.             this.TotalRecord = count;
78.             rpt.DataShow(dt); 
79.         }
80.         private Repeater getRpt()
81.         
82.             return this.Page.FindControl(this.Info.RepeaterUniqueID) as Repeater;
83.         }
先感谢一下写那个Pager的人,GetContents(得到自己分页后的HTML)里我只做了少许改动,要不然还得细细算来!!
BindData(用到了我的SqlHelper)是利用为服务器的DataBind()方法把数据放到repeater里,只是不让它呈示,嘿嘿!
getRpt只是找到Repeater引用

维护视图状态
01. #region 维护视图状态
02.         protected override void LoadViewState(object savedState)
03.         {
04.             Triplet tp = savedState as Triplet;
05.             this.TotalRecord = Convert.ToInt32(tp.Third);
06.             this.Info = tp.Second as PageInfo;
07.             base.LoadViewState(tp.First);
08.         }
09.         protected override object SaveViewState()
10.         {
11.             Triplet tp = new Triplet();
12.             tp.First = base.SaveViewState();
13.             tp.Second = Info;
14.             tp.Third = this.TotalRecord;
15.             return tp;
16.         }
17.         #endregion
这里也不必说,只是PageInfo一定要能Serializable。

重写方法
01. #region 重写方法
02.         protected override void RenderContents(HtmlTextWriter writer)
03.         {
04.             writer.Write(GetContents());
05.             base.RenderContents(writer);
06.         }
07.         protected override void AddAttributesToRender(HtmlTextWriter writer)
08.         {
09.             writer.AddStyleAttribute("White-space", "nowrap");
10.             writer.AddStyleAttribute("padding-top", "2px");
11.             writer.AddStyleAttribute("padding-bottom", "2px");
12.             writer.AddStyleAttribute("color", "#949494");
13.             writer.AddStyleAttribute("font-weight", "bold");
14.             writer.AddStyleAttribute("background-color", this.BarBackGroundColor);
15.             base.AddAttributesToRender(writer);
16.         }
17.         #endregion
也不用说,大家一看都明白。

实现ICallbackEventHandler
01. #region ICallbackEventHandler 成员
02.         public string GetCallbackResult()
03.         {
04.             StringBuilder sb=new StringBuilder ();
05.             StringWriter sw=new StringWriter (sb);
06.             getRpt().RenderControl(new HtmlTextWriter(sw));
07.             return this.GetContents() + "[_]" + sb.ToString();
08.         }
09.         public void RaiseCallbackEvent(string eventArgument)
10.         {
11.             int pageindex = int.Parse(eventArgument);
12.             this._CurrentIndex = pageindex;
13.             BindData();
14.         }
15.         #endregion
回调时先执行RaiseCallbackEvent,所以CurrentIndex改变了, BindData()执行了!!!!
返回时时执行GetCallbackResult,string用"[_]"分开,对应上面注册的AjaxPagerCallBack js方法中的var parts =returnData.split('[_]');
OK!简单的Ajax分页就这样简单的完成了!!!

Northwind Orders表调用如下:
页面中Repeater包含在<div id="AjaxData"></div>中
代码
01. private void BindPage(string content)
02.         {
03.             SinoHelper.PageInfo info = new SinoHelper.PageInfo();
04.             info.PageSize = 5;
05.             info.RepeaterUniqueID = rpt.UniqueID;
06.             info.TableName = "Orders";
07.             info.Fields = "OrderID,CustomerID,ShipCity";
08.             info.IdentityField = "OrderID";
09.             info.Content = content;
10.             AjaxPager1.Info = info;
11.             AjaxPager1.BindData();
12.         }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值