如何在ASP.Net2.0中使用quot客户端回调实现无刷新?
先贴实例代码,关于实现机制还在研究当中(比如回调是如何调用的,为什么能做到无刷新等等)
aspx文件代码:

<%...@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default6.aspx.cs"Inherits="Default6"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">
<headid="Head1"runat="server">
<title>CallbackPage</title>

<scripttype="text/javascript">...
//发起客户端回调的函数
functionGetNumber()

...{
alert("GetNumber()");
UseCallback();
}

//接收回调的客户端函数
//接收函数接受两个字符串值:一个用于返回值,另一个(可选)用于从服务器传回的上下文值。
functionGetRandomNumberFromServer(number,context)

...{
alert("GetRandomNumberFromServer");
document.forms[0].TextBox1.value=number;
}
</script>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>
<inputid="Button1"type="button"value="GetRandomNumber"onclick="GetNumber()"/>
<asp:ButtonID="BtnGetNumber"Text="GetNumber"OnClientClick="GetNumber()"runat="server"/>
</div>
</form>
</body>
</html>
后台代码:
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;

publicpartialclassDefault6:System.Web.UI.Page,System.Web.UI.ICallbackEventHandler

...{
privatestring_callbackResult=null;

protectedvoidPage_Load(objectsender,EventArgse)

...{
//判断浏览器是否支持通过Http接收XML
if(Page.Request.Browser.SupportsXmlHttp==true)

...{
//获取一个对客户端函数的引用;调用该函数时,将启动一个对服务器端事件的客户端回调。
//此重载方法的客户端函数包含:
//指定的控件(要实现ICallbackEventHandler接口)、参数(由RaiseCallbackEvent()方法返回)、客户端脚本(客户端函数名)和上下参数。
stringcbReference=Page.ClientScript.GetCallbackEventReference(this,"arg","GetRandomNumberFromServer","context");

//生成客户端脚本
stringcbScript="functionUseCallback(arg,context)"+"{"+cbReference+";"+"}";
//注册客户端脚本
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"UseCallback",cbScript,true);
}
}


ICallbackEventHandler成员#regionICallbackEventHandler成员


/**////<summary>
///回调的处理方法,ICallbackEventHandler的方法
///</summary>
///<paramname="eventArg"></param>
publicvoidRaiseCallbackEvent(stringeventArg)

...{
Randomrnd=newRandom();
_callbackResult=rnd.Next().ToString();
}


/**////<summary>
///返回回调结果,ICallbackEventHandler的方法
///</summary>
///<returns></returns>
publicstringGetCallbackResult()

...{
return_callbackResult;
}

#endregion
}
本文地址:
http://www.45fan.com/a/question/73854.html