<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> <script> function callToServer() { var param = document.getElementById("txtMessage").value; var context =""; <%= blientScript %> } function handleResultFromServer(result, context) { document.getElementById("txtResult").value = result; } </script> </head> <body> <form id="form1" runat="server"> <div> <INPUT id="txtMessage"/> <INPUT onclick="callToServer();" type="button" value="Call to Server" id="Button1"/> Result : <INPUT id="txtResult"/> </div> </form> </body> </html>
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; public partial class _Default : System .Web.UI .Page , System.Web.UI.ICallbackEventHandler { protectedvoid Page_Load(object sender, EventArgs e) { } protected String blientScript { get { returnthis.ClientScript.GetCallbackEventReference(this, "param", "handleResultFromServer", "context"); } } publicstring RaiseCallbackEvent(string eventArgument) { return"客户端在["+ DateTime.Now.ToString() +"]传送来 ["+ eventArgument +"]."; } }