2009-10-12 asp.net實現call back 機制 3099 0 AJAX asp.net callback testCallBack.vb<span style="display: none" id="1255307469931S"> </span>Partial Class testCallBack Inherits System.Web.UI.Page Implements System.Web.UI.ICallbackEventHandler Protected Overridable Function GetCallBackResult() As String _ Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult '回應給前端的資料 Return Now End Function Dim returnrs As String Sub RaiseCallbackEvent(ByVal eventArgument As String) _ Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent '前端要給後端的資料 returnrs = eventArgument End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim cbReference As String cbReference = Me.ClientScript.GetCallbackEventReference(Me, "", "ReceiveData", "") '設定call back 的觸發事件 Me.Button1.Attributes("onclick") = cbReference '自定javascript接收server端資料 Dim strjs As String strjs = "<script>" strjs &= vbCrLf & "function ReceiveData(DataFromServer){ " strjs &= vbCrLf & " document.all." & TextBox1.ClientID & ".value=DataFromServer; " strjs &= vbCrLf & " } " strjs &= vbCrLf & "</script>" Page.ClientScript.RegisterClientScriptBlock(GetType(String), "ReceiverScript", strjs) End Sub End ClasstestCallBack.aspx<%@ Page Language="VB" AutoEventWireup="false" CodeFile="testCallBack.aspx.vb" Inherits="testCallBack" %> <!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> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <input id="Button1" type="button" value="button" runat="Server" /> </div> </form> </body> </html> 測試結果: 回首頁