您的位置:首页 > 编程语言 > ASP

ASP.NET 2.0 正式版中无刷新页面的开发

2008-05-13 14:58 441 查看
下载了示例代码,并转换成VB了,AJAX功能测试成功。但遇到些小问题:

Demo1很正常。

Demo2按Leon原来的写法testAJAX.aspx.vb中Line 22不成功。所以我直接在IDE环境中,修改button3的OnClientClick属性,见testAJAX.asp的Line 52。测试成功。

Demo3按原来的加入客户端属性无法成功,在testAJAX.aspx.vb中第20行并未起作用。不知道是什么原因。 因此我只有加上一个button3来引发客户端事件。

这是个很好的例子,值得学习,详细原理说明请见作者的原文。测试时,请在您项目的web.config中添加

testAJAX.aspx

1<%@ Page Language=\"VB\" AutoEventWireup=\"false\" CodeFile=\"testAJAX.aspx.vb\" Inherits=\"testAJAX\" %>

3

5

6

7

8
9 Function CallServer1()function CallServer1(inputcontrol, context)
10 {
11 context.innerHTML = \"

Loading\";
12 arg = 'ServerMethod1|' + inputcontrol.value;
13 <%= ClientScript.GetCallbackEventReference(Me, \"arg\", \"ReceiveServerData1\", \"context\")%>;
14 }
15
16 Function ReceiveServerData1()function ReceiveServerData1(result, context)
17 {
18 context.innerHTML = context.id + \":\" + result;
19 }
20
21 Function CallServer2()function CallServer2(obj)
22 {
23 context = gridspan;
24 context.innerHTML = \"

数据加载中\";
25 arg = \"ServerMethod2|\" + obj.value;
26 <%= ClientScript.GetCallbackEventReference(Me, \"arg\", \"ReceiveServerData2\", \"context\")%>;
27 }
28
29 Function ReceiveServerData2()function ReceiveServerData2(result, context)
30 {
31 context.innerHTML = result;
32 }
33
34

35

36

37

38

39

Demo1:html按钮提交数据

40

41

42

43

44

45

46

47

48

49

50

Demo2:服务器按钮提交数据

51

52

53

54

55

56

Demo3:下拉列表框和gridview绑定数据

57 \"

58 SelectCommand=\"select distinct(country) from customers\">

59 \"

60 SelectCommand=\"select customerid, companyname, country from customers where country=@Country\">

61

62

63

64

65

66 67 DataSourceID=\"SqlDataSource1\" DataTextField=\"Country\" DataValueField=\"Country\">

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

testAJAX.aspx.vb

1

2Imports System

3Imports System.Data

4Imports System.Configuration

5Imports System.Web

6Imports System.Web.Security

7Imports System.Web.UI

8Imports System.Web.UI.WebControls

9Imports System.Web.UI.WebControls.WebParts

10Imports System.Web.UI.HtmlControls

11Imports System.IO

12Imports System.Globalization

13

14Partial Public Class testAJAXClass testAJAX

15 Inherits System.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 DropDownList1.Attributes.Add(\"onchange\", \"CallServer2(this)\")

21 'Button3的客户端事件可以直接在IDE中OnClientClick属性中设置,不需要在这里添加 by AndyDavis

22 'Button3.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 ICallbackEventHandler.GetCallbackResult

28 '为便于查看加载效果,添加延时

29 System.Threading.Thread.Sleep(2000)

30

31 Dim parts() As String = serverReturn.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 ICallbackEventHandler.RaiseCallbackEvent

38 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 arg.ToLower()

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 DropDownList1.SelectedValue = arg

62 GridView1.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(CultureInfo.InvariantCulture)

69 Dim writer2 As HtmlTextWriter = New HtmlTextWriter(writer1)

70

71 control.RenderControl(writer2)

72 writer2.Flush()

73 writer2.Close()

74

75 Return writer1.ToString()

76 End Function

77End Class
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: