原文请见Le***Zhou的:http://pfzhou.cnblogs.com/archive/2006/03/31/363342.html
下载了示例代码,并转换成VB了,AJAX功能测试成功。但遇到些小问题:
Demo1很正常。
Demo2按Leon原来的写法te***JAX.aspx.vb中Line 22不成功。所以我直接在IDE环境中,修改button3的OnClientClick属性,见te***JAX.asp的Line 52。测试成功。
Demo3按原来的加入客户端属性无法成功,在te***JAX.aspx.vb中第20行并未起作用。不知道是什么原因。 因此我只有加上一个button3来引发客户端事件。
这是个很好的例子,值得学习,详细原理说明请见作者的原文。测试时,请在您项目的we***onfig中添加
te***JAX.aspx
1<%@ Page Language="VB" AutoEventWireup="false" CodeFile="te***JAX.aspx.vb" Inherits="testAJAX" %>
2
3http://ww***.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
">
http://ww***.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5http://ww***.org/1999/xhtml">
http://ww***.org/1999/xhtml" >
6
7
ASP.NET 2.0 页面提交无刷新演示 8
35
36
37
84
85
86
te***JAX.aspx.vb 1
2Imports System
3Imports
Sy***m.Data 4Imports
Sy***m.Configuration 5Imports
Sy***m.Web 6Imports
Sy***m.Web.Security
7Imports
Sy***m.Web.UI
8Imports
Sy***m.Web.UI.WebControls
9Imports
Sy***m.Web.UI.WebControls.WebParts
10Imports
Sy***m.Web.UI.HtmlControls
11Imports
Sy***m.IO12Imports
Sy***m.Globalization13
14Partial Public Class testAJAXClass testAJAX
15 Inherits
Sy***m.Web.UI.Page
16 Implements ICallbackEventHandler
17 Protected Sub Page_Load()Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
18 '注册客户端事件处理方法
19 '似乎这句并没起作用,因此我别外加了一个button4来引发客户端事件 by AndyDavis
20
Dr***ownList1.Attributes.Add("onchange", "CallServer2(this)")
21 'Button3的客户端事件可以直接在IDE中OnClientClick属性中设置,不需要在这里添加 by AndyDavis
22 '
Bu***n3.Attributes.Add("onclick", "CallServer1(TextBox2, Label3);return false;")
23 End Sub
24
25 Private serverReturn As String
26
27 Public Function GetCallbackResult()Function GetCallbackResult() As String Implements
IC***backEventHandler.GetCallbackResult28 '为便于查看加载效果,添加延时
29
Sy***m.Threading.Thread.Sleep(2000)
30
31 Dim parts() As String =
se***rReturn.Split("|"c)
32 '根据传递的方法名进行调用,并传递相应的参数,目前只支持一个参数
33 Return CStr([GetType]().GetMethod(parts(0)).Invoke(Me, New Object() {parts(1)}))
34 End Function
35
36
37 Public Sub RaiseCallbackEvent()Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements
IC***backEventHandler.RaiseCallbackEvent38 serverReturn = eventArgument
39 End Sub
40
41 '根据从客户端传来的英文国家名或缩写,翻译成相应的中文国家名
42 Public Function ServerMethod1()Function ServerMethod1(ByVal arg As String) As String
43 Dim s As String = ""
44 Select Case
ar***oLower()
45 Case "cn"
46 Case "china"
47 s = "中国"
48 Exit Select
49 Case "us"
50 s = "美国"
51 Exit Select
52 Case Else
53 s = "未知国家"
54 Exit Select
55 End Select
56 Return s
57 End Function
58
59 '根据从客户端传来的值,对GridView的内容进行更新,并将更新后的GridView的html返回
60 Public Function ServerMethod2()Function ServerMethod2(ByVal arg As String) As String
61
Dr***ownList1.SelectedValue = arg
62
Gr***iew1.DataBind()
63
64 Return RenderControl(GridView1)
65 End Function
66
67 Private Overloads Function RenderControl()Function RenderControl(ByVal control As Control) As String
68 Dim writer1 As StringWriter = New StringWriter(
Cu***reInfo.InvariantCulture)
69 Dim writer2 As HtmlTextWriter = New HtmlTextWriter(writer1)
70
71
co***ol.RenderControl(writer2)
72
wr***r2.Flush()
73
wr***r2.Close()
74
75 Return
wr***r1.ToString()
76 End Function
77End Class
78