2009-10-12 asp.net callback 自定timer 2344 0 AJAX 摘要:asp.net callback 自定timer testcallback.vbPartial 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", "") Dim strjs As String strjs = "<script>" strjs &= vbCrLf & "function ReceiveData(DataFromServer){ " strjs &= vbCrLf & " document.all." & TextBox1.ClientID & ".value=DataFromServer; " strjs &= vbCrLf & " } " '每秒 reload 一次 strjs &= vbCrLf & " function countSecond(){" strjs &= vbCrLf & cbReference strjs &= vbCrLf & " setTimeout(""countSecond()"", 1000); " strjs &= vbCrLf & "} " strjs &= vbCrLf & "countSecond()" strjs &= vbCrLf & "</script>" Page.ClientScript.RegisterClientScriptBlock(GetType(String), "ReceiverScript", strjs) End Sub EndClasstestcallback.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> </div> </form> </body> </html>執行結果 回首頁