非Ajax实现客户端的异步调用
先来看 default.aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 language="javascript" type="text/javascript">
function GetNumber()
{
UseCallback();
}
function GetRandomNumberFromServer(values,content)
{
document.getElementById("TextBox1").value=values;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="Button1" type="button" value="button" onclick="GetNumber()" /></div>
</form>
</body>
</html>
<!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 language="javascript" type="text/javascript">
function GetNumber()
{
UseCallback();
}
function GetRandomNumberFromServer(values,content)
{
document.getElementById("TextBox1").value=values;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input id="Button1" type="button" value="button" onclick="GetNumber()" /></div>
</form>
</body>
</html>
接下来看deault.cs页面代码:
using System;
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
private string a = null;
protected void Page_Load(object sender, EventArgs e)
{
string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "GetRandomNumberFromServer", "context");
string cbScript = "function UseCallback(arg,context)" + "{" + cbReference + ";" + "}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallback", cbScript, true);
}
public string GetCallbackResult()
{
return a;
}
public void RaiseCallbackEvent(string enentArg)
{
Random rd = new Random();
a = rd.Next().ToString();
}
}
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{
private string a = null;
protected void Page_Load(object sender, EventArgs e)
{
string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "GetRandomNumberFromServer", "context");
string cbScript = "function UseCallback(arg,context)" + "{" + cbReference + ";" + "}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "UseCallback", cbScript, true);
}
public string GetCallbackResult()
{
return a;
}
public void RaiseCallbackEvent(string enentArg)
{
Random rd = new Random();
a = rd.Next().ToString();
}
}
哈哈,就这么简单,点击标准html按钮就会自动获取后台自动生成的一个随机数,并显示到页面上,哈哈,是不是很强大~!!!