62,266
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="callback.aspx.cs" Inherits="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>无标题页</title>
<script type="text/javascript">
//接收服务器返回的数据,在页面显示出来。
function ReceiveServerData(arg, context)
{
var node=window.setTimeout("CallTheServer()",1000);
try
{
document.getElementById("msg_display").style.width=arg;
//document.getElementById("msg_display").innerHTML=arg;
}
catch(e)
{
alert(e);
}
}
//向服务器发送请求
function CallTheServer(arg, context)
{
<%= ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context") %>;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<br />
<button onclick="CallTheServer()">生成</button>
<div id="msg_display" style="background: yellow; font-weight: bold; font-size: 13px">
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Threading;
public partial class callback : System.Web.UI.Page,ICallbackEventHandler
{
//定义一个全局字符串供callback使用
string str = "";
public int id = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
// 定义当callback服务器时回调的方法
public void RaiseCallbackEvent(String eventArgument)
{
str = "10";
int total = 30;
for (id = 0; id < total; id++)
{
str += id;
Thread.Sleep(1000);
}
}
// 定义返回给CallBack的字符串
public string GetCallbackResult()
{
return str;
}
}