当前位置:首页 > asp.net在Client端使用另一种异步技术CallBack 注:非ajax方式

asp.net在Client端使用另一种异步技术CallBack 注:非ajax方式

点击次数:1187  更新日期:2010-12-30
\n

第一步(在页面上创建“输入”和“输出”区域):myPage.aspx
<form id=”form1″ runat=”server”>
<div>
输入姓名:<input id=”txtUserName” type=”text” />
输出信息:<span id=”Results” ></span>
</div>
</form>
第二步(分别创建两个javascript方法通过DOM控制“输入”和“输出”):myPage.aspx
<script type=”text/JavaScript”>
function DoSearch()
{
var txtFirstName = document.getElementById(“txtUserName”);
CallServer(txtFirstName.value, “”);
}

function ReceiveServerData(txtUserInfo)
{
Results.innerText = txtUserInfo;
}

setInterval(‘DoSearch()’,1000);
</script>
第三步(在后台注册CallServer和ReceiveServerData):myPage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{

String cbReference = Page.ClientScript.GetCallbackEventReference(this, “arg”, “ReceiveServerData”, “context”);
String callbackScript;
callbackScript = “function CallServer(arg, context)” + “{ ” + cbReference + “} ;”;
//注册JavaScript
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), “CallServer”, callbackScript, true);
}
第四步(后台继承ICallbackEventHandler并实现RaiseCallbackEvent()和GetCallbackResult())
//引发Callback事件处理
public partial class myPage : System.Web.UI.Page,ICallbackEventHandler
{
protected string txtUserInfo;//信息


\n

public void RaiseCallbackEvent(string txtFirstName)
{
txtUserInfo=”信息”;
}
//返回Callback结果
public string GetCallbackResult()
{
return txtUserInfo; //返回信息
}
}
前台myPage.aspx完整代码:
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Page.aspx.cs” Inherits=”myPage” %>


\n

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


\n

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>Page</title>
<script type=”text/JavaScript”>
function DoSearch()
{
var txtFirstName = document.getElementById(“txtUserName”);
CallServer(txtFirstName.value, “”);
}

function ReceiveServerData(txtUserInfo)
{
Results.innerText = txtUserInfo;
}

setInterval(‘DoSearch()’,1000);
</script>


\n

</head>
<body>
<form id=”form1″ runat=”server”>
<div>
输入姓名:<input id=”txtUserName” type=”text” />
输出信息:<span id=”Results” ></span>
</div>
</form>
</body>
</html>


\n

后台myPage.aspx.cs完整代码:


\n

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.Data.SqlClient;
public partial class myPage : System.Web.UI.Page,ICallbackEventHandler
{
protected string txtUserInfo;//信息


\n

protected void Page_Load(object sender, EventArgs e)
{

String cbReference = Page.ClientScript.GetCallbackEventReference(this, “arg”, “ReceiveServerData”, “context”);
String callbackScript;
callbackScript = “function CallServer(arg, context)” + “{ ” + cbReference + “} ;”;


\n

//注册JavaScript
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), “CallServer”, callbackScript, true);
}


\n

public void RaiseCallbackEvent(string txtFirstName)
{
txtUserInfo=”信息”;
}
//返回Callback结果
public string GetCallbackResult()
{
return txtUserInfo; //返回信息
}
}

来源:http://www.cnblogs.com/ecngs

\n