代码改变世界

Ajax技术三种实现方式之asp.net2.0 callback篇 (四)

2011-04-15 12:01  沐海  阅读(270)  评论(0编辑  收藏  举报
View Code
1 一、 Asp.net2.0的形式:CallBack
2 1、CallBack.aspx
3 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CallBack.aspx.cs" Inherits="AJAX.CallBack" %>
4
5 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6
7 <html xmlns="http://www.w3.org/1999/xhtml" >
8 <head runat="server">
9 <title>Untitled Page</title>
10 <mce:script language="javascript" type="text/javascript"><!--
11 function ReceiveServerData(arg, context)
12 {
13 document.getElementById ("msg_display").innerHTML=arg;
14 }
15 function CallTheServer(arg,context)
16 {
17 <%=ClientScript.GetCallbackEventReference(this,"arg", "ReceiveServerData", "context") %>
18 }
19 // --></mce:script>
20 </head>
21 <body>
22 <form id="form1" runat="server">
23 <div>
24 <input id="testmsg" type="text" value="Hello,World!" style="width: 214px"><br />
25 <input type="button" value="單擊這個" onclick="CallTheServer(document.getElementById('testmsg').value)" />
26 提交给服务器的CallBack
27
28 <div id="msg_display" style="background: yellow; font-weight: bold; font-size: 13px" mce_style="background: yellow; font-weight: bold; font-size: 13px">
29 从服务器返回的信息在这里显示...</div>
30 </div>
31 </form>
32 </body>
33 </html>
34
35 2、CallBack.aspx.cs
36 using System;
37 using System.Collections;
38 using System.Configuration;
39 using System.Data;
40 using System.Linq;
41 using System.Web;
42 using System.Web.Security;
43 using System.Web.UI;
44 using System.Web.UI.HtmlControls;
45 using System.Web.UI.WebControls;
46 using System.Web.UI.WebControls.WebParts;
47 using System.Xml.Linq;
48 using System.Text;
49 namespace AJAX
50 {
51 public partial class CallBack : System.Web.UI.Page,ICallbackEventHandler
52 {
53 protected void Page_Load(object sender, EventArgs e)
54 {
55
56 }
57 // Summary:
58 // Returns the results of a callback event that targets a control.
59 //
60 // Returns:
61 // The result of the callback.
62 StringBuilder builder = new StringBuilder();
63 public string GetCallbackResult()
64 {
65 return builder.ToString();
66 }
67 //
68 // Summary:
69 // Processes a callback event that targets a control.
70 //
71 // Parameters:
72 // eventArgument:
73 // A string that represents an event argument to pass to the event handler.
74 public void RaiseCallbackEvent(string eventArgument)
75 {
76 builder.Append("服务器callback得到了您输入的信息:" + eventArgument + "<br/>您的IP地址是:");
77 builder.Append(Request.UserHostAddress);
78 builder.Append("<br/>当前服务器的时间:");
79 builder.Append(DateTime.Now.ToLocalTime());
80 }
81 }
82 }
记录生活、工作、学习点滴!
E-Mail:mahaisong@hotmail.com 欢迎大家讨论。
沐海博客园,我有一颗,卓越的心!