日期:2011-09-27 浏览次数:20443 次
WebForm_DoCallback('__Page',arg,ReceiveServerData,context,null,false) |
<%@ page language="C#" CodeFile="ServerTime.aspx.cs" Inherits="ServerTime_aspx" %> <html> <head> <title>Server Time</title> <script language="javascript"> function GetServerTime() { var message = ''; var context = ''; <%=sCallBackFunctionInvocation%> } function ShowServerTime(timeMessage, context) { alert('现在服务器上的时间是:\n' + timeMessage); } </script> </head> <body> <form id="MainForm" runat="server"> <input type="button" value="得到服务器端时间" /> </form> </body> </html> |
using System; using System.Web.UI; public partial class ServerTime_aspx : Page,ICallbackEventHandler { //一定要实现ICallbackEventHandler借口 public string sCallBackFunctionInvocation; void Page_Load(object sender, System.EventArgs e) { sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this, "message", "ShowServerTime", "context"); } public string RaiseCallbackEvent(string eventArgument) { return DateTime.Now.ToString(); } } |
第二种方法:在上面的方法中我们必须要在前台绑定后台,那么如果不绑定呢?我们这样做:
直接把GetCallbackEventReference当做js函数中的一个实现内容,然后把这个js函数注册到客户端。
前台TestPage代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="TestPage" %> <html> <head> <title>Untitled Page</title> <script type="text/javascript"> function test() { var lb = document.getElementById("Select1"); //取的那个下拉框 var con = lb.options[lb.selectedIndex].text; //得到你选择的下拉框的文本再调用呢个CallTheServer,是一个由服务器端输出的js函数 CallTheServer(con,''); } function ReceiveServerData(rValue) { Results.innerHTML = rValue; } </script> </head> <body> <form id="form1" runat="server"> <div> <select id="Select1"> <option value=1 selected="selected">老鼠徒弟</option> <option value=2>吴旗娃师傅</option> </select> <br /> <br /> <input value="从服务器返回下拉框文本" type=button> <br /> <br /> <span ID="Results"></span> <br /> </div> </form> </body> </html> |