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

c# 通过ICallbackEventHandler 实现页面无刷新

2011-08-14 22:43 274 查看

创建服务器回调方法

在服务器代码中,必须创建一个实现RaiseCallbackEvent方法的方法和一个实现GetCallbackResult方法的方法。RaiseCallbackEvent方法接受单个字符串参数,而不是通常用于事件处理程序的常见的两个参数。该方法的部分内容可能与下面的代码示例类似。

publicvoidRaiseCallbackEvent(StringeventArgument)

{


}

GetCallbackResult不接受参数并返回一个字符串。该方法的部分内容可能与下面的代码示例类

publicstringGetCallbackResult()

{

returnaStringValue;

}


Aspx:

<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="WebForm1.aspx.cs"Inherits="WebTest.WebForm1"%>
<%@ImportNamespace="System.Web.UI"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<scripttype="text/jscript">
functionCallServer(inputcontrol,context)
{
//回调还没有处理完全时其预先加载的显示值
context.innerHTML="加载中...";
//这里的arg传递到后台RaiseCallbackEvent(stringeventArgument)方法中去
arg=inputcontrol.value;
//获取一个对客户端函数的引用调用该函数时将启动一个对服务器端事件的客户端回调

//各参数含义:
//this指当前页面
//arg客户端传递给服务端RaiseCallbackEvent的值
//ReceiveServerData客户端的一个js函数,服务器会返回值传递给这个函数,做为这个函数的参数。
//context脚本的结果传回客户端事件处理程序
<%=ClientScript.GetCallbackEventReference(this,"arg","ReceiveServerData","context")%>
}
functionReceiveServerData(result,context)
{
context.innerHTML=result;
}
</script>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headid="Head1"runat="server">
<title>无标题页</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<inputid="Text1"type="text"/>
<inputid="Button1"type="button"value="button"onclick="CallServer(Text1,Label1);"/>
</div>
<asp:LabelID="Label1"runat="server"Text="Label"></asp:Label>
</form>
</body>
</html>

Aspx.cs

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;

namespaceWebTest
{
publicpartialclassWebForm1:System.Web.UI.Page,ICallbackEventHandler
{
stringtestvalue="";
protectedvoidPage_Load(objectsender,EventArgse)
{

}

#regionICallbackEventHandler成员

publicstringGetCallbackResult()
{
returntestvalue;
}

publicvoidRaiseCallbackEvent(stringeventArgument)
{
testvalue=eventArgument+"1";
}

#endregion
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: