首先说一下实现的依据,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>
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
using System;
2
using System.Data;
3
using System.Web;
4
using System.Text;
5
using System.Web.UI;
6
using System.Web.UI.HtmlControls;
7
using System.Web.UI.WebControls;
8
using System.IO;
9
using System.Globalization;
10
11
public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
12
}
2
3
4
5
6
7
8
9
10
11
12
正如代码所示,需要实现ICallbackEventHandler接口的两个方法,RaiseCallbackEvent用于接收到客户端传回来的参数,GetCallbackResult是在服务端做出合适的反应后将指定的结果发送回客户端,这里是把repeater重画的内容返回了,然后在客户端ReceiveCallback函数里进行无刷新的最后展现,最想说明的是,一定要注意CallServer(pageIndex,'content');ReceiveCallback(arg,context);这两个函数的使用技巧,CallServer是服务端生成的,它的第二个参数直接决定了在什么地方无刷新,ReceiveCallback和前者都是context参数,内容就是相同的,相当于传到服务器再原封不动的传回来,因此用于回发和接收时共享数据。
小示例可变换多种实现,希望大家都能找到适合自己简便实现的办法。