代码改变世界

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

  沐海  阅读(277)  评论(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 }
复制代码
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
记录生活、工作、学习点滴!
E-Mail:mahaisong@hotmail.com 欢迎大家讨论。
沐海博客园,我有一颗,卓越的心!
点击右上角即可分享
微信分享提示