以下的範例就是撰寫一個支援 CallBack 回呼的 BasePage,當頁面執行 CallBack 回呼時,會引發 CallBack 事件,開發人員只需在 CallBack 事件中判斷傳入值,並處理回傳值。
先撰寫一個實作 實作 System.Web.UI.ICallbackEventHandler 介面的 TBBasePage,其中 CallBack 回呼時會引發「CallBack 事件」,CallBackEventArgs 事件引數包含 Argument 屬性(CallBack 傳入參數) 及 Result 屬性 (CallBack 回傳結果)。
1 Imports Microsoft.VisualBasic
2
3 Public Class TBBasePage
4 Inherits System.Web.UI.Page
5 Implements System.Web.UI.ICallbackEventHandler
6
7 Private FEventArgument As String = String.Empty
8
9 #Region " CallBack 事件 "
10
11 ''' <summary>
12 ''' CallBack 事件引數。
13 ''' </summary>
14 Public Class CallBackEventArgs
15 Inherits System.EventArgs
16
17 Private FArgument As String = String.Empty
18 Private FResult As String = String.Empty
19
20 ''' <summary>
21 ''' CallBack 傳入參數。
22 ''' </summary>
23 Public Property Argument() As String
24 Get
25 Return FArgument
26 End Get
27 Set(ByVal value As String)
28 FArgument = value
29 End Set
30 End Property
31
32 ''' <summary>
33 ''' CallBack 回傳結果。
34 ''' </summary>
35 Public Property Result() As String
36 Get
37 Return FResult
38 End Get
39 Set(ByVal value As String)
40 FResult = value
41 End Set
42 End Property
43 End Class
44
45 ''' <summary>
46 ''' 執行 CallBack 回傳的事件。
47 ''' </summary>
48 Public Event CallBack(ByVal sender As Object, ByVal e As CallBackEventArgs)
49
50 #End Region
51
52 Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
53 Dim e As New CallBackEventArgs()
54
55 e.Argument = FEventArgument
56 RaiseEvent CallBack(Me, e) '引發 CallBack 事件
57 Return e.Result
58 End Function
59
60 Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
61 FEventArgument = eventArgument
62 End Sub
63 End Class
64
2
3 Public Class TBBasePage
4 Inherits System.Web.UI.Page
5 Implements System.Web.UI.ICallbackEventHandler
6
7 Private FEventArgument As String = String.Empty
8
9 #Region " CallBack 事件 "
10
11 ''' <summary>
12 ''' CallBack 事件引數。
13 ''' </summary>
14 Public Class CallBackEventArgs
15 Inherits System.EventArgs
16
17 Private FArgument As String = String.Empty
18 Private FResult As String = String.Empty
19
20 ''' <summary>
21 ''' CallBack 傳入參數。
22 ''' </summary>
23 Public Property Argument() As String
24 Get
25 Return FArgument
26 End Get
27 Set(ByVal value As String)
28 FArgument = value
29 End Set
30 End Property
31
32 ''' <summary>
33 ''' CallBack 回傳結果。
34 ''' </summary>
35 Public Property Result() As String
36 Get
37 Return FResult
38 End Get
39 Set(ByVal value As String)
40 FResult = value
41 End Set
42 End Property
43 End Class
44
45 ''' <summary>
46 ''' 執行 CallBack 回傳的事件。
47 ''' </summary>
48 Public Event CallBack(ByVal sender As Object, ByVal e As CallBackEventArgs)
49
50 #End Region
51
52 Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
53 Dim e As New CallBackEventArgs()
54
55 e.Argument = FEventArgument
56 RaiseEvent CallBack(Me, e) '引發 CallBack 事件
57 Return e.Result
58 End Function
59
60 Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
61 FEventArgument = eventArgument
62 End Sub
63 End Class
64
當頁面需要 CallBack 時,只需要繼承 TBBasePage,在「CallBack 事件」中以 e.Argument 判斷傳入參數,並將回傳結果寫入 e.Result。以下範例就是繼承 TBBasePage,利用 CallBack 方式「取得伺服端時間」。
*.aspx.vb
1 Partial Class _Default
2 Inherits TBBasePage
3
4 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
5 'Button1 的 onclick 執行 CallBack 回伺服端
6 btnCallBackButton.Attributes("onclick") = Me.ClientScript.GetCallbackEventReference(Me, "", "ReceiveServerData", "")
7 End Sub
8
9 ''' <summary>
10 ''' CallBack 的回呼事件。
11 ''' </summary>
12 Protected Sub Page_CallBack(ByVal sender As Object, ByVal e As TBBasePage.CallBackEventArgs) Handles Me.CallBack
13 e.Result = Now.ToString() '傳回伺服端時間
14 End Sub
15 End Class
16
2 Inherits TBBasePage
3
4 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
5 'Button1 的 onclick 執行 CallBack 回伺服端
6 btnCallBackButton.Attributes("onclick") = Me.ClientScript.GetCallbackEventReference(Me, "", "ReceiveServerData", "")
7 End Sub
8
9 ''' <summary>
10 ''' CallBack 的回呼事件。
11 ''' </summary>
12 Protected Sub Page_CallBack(ByVal sender As Object, ByVal e As TBBasePage.CallBackEventArgs) Handles Me.CallBack
13 e.Result = Now.ToString() '傳回伺服端時間
14 End Sub
15 End Class
16
*.aspx 程式碼如下,其中 ReceiveServerData 為接收 CallBack 回傳的 JavaScript 函式。
1 <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head runat="server">
6 <title>未命名頁面</title>
7
8 <script type="text/javascript">
9 function ReceiveServerData(rValue)
10 {
11 var o = document.getElementById("Label1");
12 o.innerText = rValue;
13 }
14 </script>
15
16 </head>
17 <body>
18 <form id="form1" runat="server">
19 <div>
20 <input id="btnCallBackButton" runat="server" type="button" value="CallBack Click" /><br />
21 Server Time:
22 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
23 </form>
24 </body>
25 </html>
26
27 </script>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head runat="server">
6 <title>未命名頁面</title>
7
8 <script type="text/javascript">
9 function ReceiveServerData(rValue)
10 {
11 var o = document.getElementById("Label1");
12 o.innerText = rValue;
13 }
14 </script>
15
16 </head>
17 <body>
18 <form id="form1" runat="server">
19 <div>
20 <input id="btnCallBackButton" runat="server" type="button" value="CallBack Click" /><br />
21 Server Time:
22 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
23 </form>
24 </body>
25 </html>
26
27 </script>