您的位置:首页 > 编程语言 > ASP

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

2011-04-15 12:01 447 查看
View Code

一、             Asp.net2.0的形式:CallBack
1、CallBack.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CallBack.aspx.cs" Inherits="AJAX.CallBack" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<mce:script language="javascript" type="text/javascript"><!--
function ReceiveServerData(arg, context)
{
document.getElementById ("msg_display").innerHTML=arg;
}
function CallTheServer(arg,context)
{
<%=ClientScript.GetCallbackEventReference(this,"arg", "ReceiveServerData", "context") %>
}
// --></mce:script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="testmsg" type="text" value="Hello,World!" style="width: 214px"><br />
<input type="button" value="單擊這個" onclick="CallTheServer(document.getElementById('testmsg').value)" />
提交给服务器的CallBack

<div id="msg_display" style="background: yellow; font-weight: bold; font-size: 13px" mce_style="background: yellow; font-weight: bold; font-size: 13px">
从服务器返回的信息在这里显示...</div>
</div>
</form>
</body>
</html>

2、CallBack.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
namespace AJAX
{
public partial class CallBack : System.Web.UI.Page,ICallbackEventHandler
{
protected void Page_Load(object sender, EventArgs e)
{

}
// Summary:
//     Returns the results of a callback event that targets a control.
//
// Returns:
//     The result of the callback.
StringBuilder builder = new StringBuilder();
public string GetCallbackResult()
{
return builder.ToString();
}
//
// Summary:
//     Processes a callback event that targets a control.
//
// Parameters:
//   eventArgument:
//     A string that represents an event argument to pass to the event handler.
public void RaiseCallbackEvent(string eventArgument)
{
builder.Append("服务器callback得到了您输入的信息:" + eventArgument + "<br/>您的IP地址是:");
builder.Append(Request.UserHostAddress);
builder.Append("<br/>当前服务器的时间:");
builder.Append(DateTime.Now.ToLocalTime());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: