客户端脚本简单实现Repeater的无刷新分页

  最近有朋友在问,如果不使用ajax.net,ajaxpro等等开发包,有没有别的办法写客户端脚本让Repeater实现无刷新分页,答案当然是有的。其他那些开发包无非是帮我们完成好了回发的各种接口,可是,开发包面向的是通用的情况,然不得已需要引入的脚本库有相当的容量,但我们不需要全部,现介绍一种最轻量级的无刷新分页办法(以Repeater为例)。
  首先说一下实现的依据,asp.net中有这样一个方法GetCallbackEventReference。它的作用是获取一个对客户端函数的引用;调用该函数时,将启动一个对服务器事件的客户端回调。先可以不用理解透这句话的意思,只需要记住它能获取一段脚本,让客户端调用后实现回发到服务端。废话不多说了,赶紧跟着操作如下:
  建立一个WebSite,当然你要用WebProject也无妨,然后增加一个WebForm,假定名为default。
  default.aspx内容是
  
 1  <% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " Default.aspx.cs "  Inherits = " _Default "  
 2 
 3  %>
 4 
 5  <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 6 
 7  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
 8  < html  xmlns ="http://www.w3.org/1999/xhtml" >
 9  < head  runat ="server" >
10       < title > CallBack </ title >
11 
12       < script  language ="javascript"  type ="text/javascript" >
13       function  turnPage(pageIndex){
14          CallServer(pageIndex, ' content ' );
15      }
16      
17       function  ReceiveCallback(arg,context){
18           var  container  =  document.getElementById(context);
19           // alert(arg + "  " + context);
20          container.innerHTML  =  arg;
21      }
22       </ script >
23 
24  </ head >
25  < body >
26       < form  id ="form1"  runat ="server" >
27       < div  id ="content" >
28           < asp:Repeater  ID ="List"  runat ="server"  OnItemDataBound ="List_ItemDataBound" >
29               < ItemTemplate >
30                   < div >
31                      用户名: < asp:Label  ID ="NickName"  runat ="server" ></ asp:Label >
32                      QQ号: < asp:Label  ID ="QNumber"  runat ="server" ></ asp:Label >
33                   </ div >
34               </ ItemTemplate >
35           </ asp:Repeater >
36       </ div >
37       < asp:Literal  ID ="Pager"  runat ="server" ></ asp:Literal >
38       </ form >
39  </ body >
40  </ html >

default.aspx.cs内容是
 
  1 None.gif using  System;
  2 None.gif using  System.Data;
  3 None.gif using  System.Web;
  4 None.gif using  System.Text;
  5 None.gif using  System.Web.UI;
  6 None.gif using  System.Web.UI.HtmlControls;
  7 None.gif using  System.Web.UI.WebControls;
  8 None.gif using  System.IO;
  9 None.gif using  System.Globalization;
 10 None.gif
 11 None.gif public   partial   class  _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
 12 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 13InBlock.gif    //每页显示记录数
 14InBlock.gif    static int PAGESIZE = 4;
 15InBlock.gif    DataTable dt = null;
 16InBlock.gif    private int currentPageIndex;
 17InBlock.gif    protected void Page_Load(object sender, EventArgs e)
 18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 19InBlock.gif        if (!IsPostBack)
 20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 21InBlock.gif            BindList(1true);
 22InBlock.gif           //获取用于回调的
 23InBlock.gif            string callbackReference = ClientScript.GetCallbackEventReference(this"arg"
 24InBlock.gif
 25InBlock.gif"ReceiveCallback""context"false);
 26InBlock.gif            string callbackScript = string.Format("function CallServer(arg,context){{ {0} 
 27InBlock.gif
 28ExpandedSubBlockEnd.gif}
}
", callbackReference);
 29InBlock.gif            ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer"
 30InBlock.gif
 31InBlock.gifcallbackScript, true);
 32InBlock.gif
 33ExpandedBlockEnd.gif        }

 34 None.gif    }
 35 None.gif
 36 ExpandedBlockStart.gifContractedBlock.gif     /**/ /// <summary>
 37InBlock.gif    /// 绑定列表
 38InBlock.gif    /// </summary>
 39InBlock.gif    /// <param name="pageIndex">翻页页码</param>
 40ExpandedBlockEnd.gif    /// <param name="needRender">是否需要重画分页面码</param>

 41 None.gif      protected   void  BindList( int  pageIndex,  bool  needRender)
 42 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 43InBlock.gif        DataTable dt = GetData();
 44InBlock.gif        //计算总页数
 45InBlock.gif        int pages = (dt.Rows.Count % PAGESIZE == 0? dt.Rows.Count / PAGESIZE : 
 46InBlock.gif
 47InBlock.gif(dt.Rows.Count / PAGESIZE) + 1;
 48InBlock.gif        if (needRender)
 49InBlock.gif            RenderPager(pages);
 50InBlock.gif
 51InBlock.gif        if (pageIndex > pages)
 52InBlock.gif            pageIndex = pages;
 53InBlock.gif        else if (pageIndex < 1)
 54InBlock.gif            pageIndex = 1;
 55InBlock.gif        int startId = (pageIndex - 1* PAGESIZE + 1;
 56InBlock.gif        int endId = pageIndex * PAGESIZE;
 57InBlock.gif
 58InBlock.gif        DataRow[] rows = dt.Select(string.Format("id>={0} and id<={1}", startId, endId));
 59InBlock.gif        List.DataSource = rows;
 60InBlock.gif        List.DataBind();
 61ExpandedBlockEnd.gif    }

 62 None.gif
 63 ExpandedBlockStart.gifContractedBlock.gif     /**/ /// <summary>
 64InBlock.gif    /// 画出分页页码
 65InBlock.gif    /// </summary>
 66ExpandedBlockEnd.gif    /// <param name="pages"></param>

 67 None.gif      protected   void  RenderPager( int  pages)
 68 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 69InBlock.gif        StringBuilder sb = new StringBuilder();
 70InBlock.gif        int pageIndex = 1;
 71InBlock.gif        do
 72ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 73InBlock.gif            sb.AppendFormat("<a href='javascript:turnPage({0});'>{0}</a>&nbsp;&nbsp;"
 74InBlock.gif
 75InBlock.gifpageIndex);
 76ExpandedSubBlockEnd.gif        }
 while (pageIndex++ < pages);
 77InBlock.gif
 78InBlock.gif        Pager.Text = sb.ToString();
 79ExpandedBlockEnd.gif    }

 80 None.gif
 81 ExpandedBlockStart.gifContractedBlock.gif     /**/ /// <summary>
 82InBlock.gif    /// 初始化一个DataTable作数据源
 83ExpandedBlockEnd.gif    /// </summary>

 84 None.gif      protected  DataTable GetData()
 85 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 86InBlock.gif        if (null == Cache["Data"])
 87ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 88InBlock.gif            dt = new DataTable();
 89InBlock.gif            dt.Columns.Add("ID"typeof(int));
 90InBlock.gif            dt.Columns.Add("NickName"typeof(string));
 91InBlock.gif            dt.Columns.Add("QNumber"typeof(string));
 92InBlock.gif
 93InBlock.gif            DataRow row = dt.NewRow();
 94InBlock.gif            row["ID"= 1;
 95InBlock.gif            row["NickName"= "人物1";
 96InBlock.gif            row["QNumber"= "21243468";
 97InBlock.gif            dt.Rows.Add(row);
 98InBlock.gif
 99InBlock.gif            row = dt.NewRow();
100InBlock.gif            row["ID"= 2;
101InBlock.gif            row["NickName"= "人物2";
102InBlock.gif            row["QNumber"= "9058307";
103InBlock.gif            dt.Rows.Add(row);
104InBlock.gif
105InBlock.gif            row = dt.NewRow();
106InBlock.gif            row["ID"= 3;
107InBlock.gif            row["NickName"= "人物3";
108InBlock.gif            row["QNumber"= "21243468";
109InBlock.gif            dt.Rows.Add(row);
110InBlock.gif
111InBlock.gif            row = dt.NewRow();
112InBlock.gif            row["ID"= 4;
113InBlock.gif            row["NickName"= "人物4";
114InBlock.gif            row["QNumber"= "22526451";
115InBlock.gif            dt.Rows.Add(row);
116InBlock.gif
117InBlock.gif            row = dt.NewRow();
118InBlock.gif            row["ID"= 5;
119InBlock.gif            row["NickName"= "人物5";
120InBlock.gif            row["QNumber"= "254852182";
121InBlock.gif            dt.Rows.Add(row);
122InBlock.gif
123InBlock.gif            row = dt.NewRow();
124InBlock.gif            row["ID"= 6;
125InBlock.gif            row["NickName"= "人物6";
126InBlock.gif            row["QNumber"= "81461006";
127InBlock.gif            dt.Rows.Add(row);
128InBlock.gif
129InBlock.gif            row = dt.NewRow();
130InBlock.gif            row["ID"= 7;
131InBlock.gif            row["NickName"= "人物7";
132InBlock.gif            row["QNumber"= "375772376";
133InBlock.gif            dt.Rows.Add(row);
134InBlock.gif
135InBlock.gif            row = dt.NewRow();
136InBlock.gif            row["ID"= 8;
137InBlock.gif            row["NickName"= "人物8";
138InBlock.gif            row["QNumber"= "153534649";
139InBlock.gif            dt.Rows.Add(row);
140InBlock.gif
141InBlock.gif            row = dt.NewRow();
142InBlock.gif            row["ID"= 9;
143InBlock.gif            row["NickName"= "人物9";
144InBlock.gif            row["QNumber"= "619468";
145InBlock.gif            dt.Rows.Add(row);
146InBlock.gif
147InBlock.gif            row = dt.NewRow();
148InBlock.gif            row["ID"= 10;
149InBlock.gif            row["NickName"= "人物10";
150InBlock.gif            row["QNumber"= "83223563";
151InBlock.gif            dt.Rows.Add(row);
152InBlock.gif
153InBlock.gif            Cache["Data"= dt;
154ExpandedSubBlockEnd.gif        }

155InBlock.gif        else
156ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
157InBlock.gif            dt = Cache["Data"as DataTable;
158ExpandedSubBlockEnd.gif        }

159InBlock.gif        return dt;
160ExpandedBlockEnd.gif    }

161 None.gif
162 ContractedBlock.gifExpandedBlockStart.gif     实现ICallbackEventHandler 成员RaiseCallbackEvent #region 实现ICallbackEventHandler 成员RaiseCallbackEvent
163InBlock.gif
164InBlock.gif    public void RaiseCallbackEvent(string eventArgument)
165ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
166InBlock.gif        currentPageIndex = Convert.ToInt32(eventArgument);
167InBlock.gif        BindList(currentPageIndex, false);
168ExpandedSubBlockEnd.gif    }

169InBlock.gif
170InBlock.gif    public string GetCallbackResult()
171ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
172InBlock.gif        //返回再次绑定后的Repeater的表现内容
173InBlock.gif        return RenderControl(List);
174ExpandedSubBlockEnd.gif    }

175ExpandedBlockEnd.gif    #endregion

176 None.gif
177 None.gif     protected   void  List_ItemDataBound( object  sender, RepeaterItemEventArgs e)
178 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
179InBlock.gif        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == 
180InBlock.gif
181InBlock.gifListItemType.AlternatingItem)
182ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
183InBlock.gif            Label NickName = e.Item.FindControl("NickName"as Label;
184InBlock.gif            Label QNumber = e.Item.FindControl("QNumber"as Label;
185InBlock.gif
186InBlock.gif            DataRow row = e.Item.DataItem as DataRow;
187InBlock.gif            NickName.Text = row["NickName"].ToString();
188InBlock.gif            QNumber.Text = row["QNumber"].ToString();
189ExpandedSubBlockEnd.gif        }

190ExpandedBlockEnd.gif    }

191 None.gif
192 ExpandedBlockStart.gifContractedBlock.gif     /**/ /// <summary>
193InBlock.gif    /// 获取指定控件重画的内容
194InBlock.gif    /// </summary>
195InBlock.gif    /// <param name="control"></param>
196ExpandedBlockEnd.gif    /// <returns></returns>

197 None.gif      private   string  RenderControl(Control control)
198 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
199InBlock.gif        StringWriter writer1 = new StringWriter(CultureInfo.InvariantCulture);
200InBlock.gif        HtmlTextWriter writer2 = new HtmlTextWriter(writer1);
201InBlock.gif
202InBlock.gif        control.RenderControl(writer2);
203InBlock.gif        writer2.Flush();
204InBlock.gif        writer2.Close();
205InBlock.gif
206InBlock.gif        return writer1.ToString();
207ExpandedBlockEnd.gif    }

208 None.gif}

  正如代码所示,需要实现ICallbackEventHandler接口的两个方法,RaiseCallbackEvent用于接收到客户端传回来的参数,GetCallbackResult是在服务端做出合适的反应后将指定的结果发送回客户端,这里是把repeater重画的内容返回了,然后在客户端ReceiveCallback函数里进行无刷新的最后展现,最想说明的是,一定要注意CallServer(pageIndex,'content');ReceiveCallback(arg,context);这两个函数的使用技巧,CallServer是服务端生成的,它的第二个参数直接决定了在什么地方无刷新,ReceiveCallback和前者都是context参数,内容就是相同的,相当于传到服务器再原封不动的传回来,因此用于回发和接收时共享数据。
  小示例可变换多种实现,希望大家都能找到适合自己简便实现的办法。

转载于:https://www.cnblogs.com/BeanHsiang/archive/2008/04/17/1158281.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值