原文请见leon.zhou的:http://pfzhou.cnblogs.com/archive/2006/03/31/363342.html
下载了示例代码,并转换成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中添加<add name="northwind" connectionstring="...相应的数据库连接串...">
testajax.aspx
1<%@ page language="vb" autoeventwireup="false" codefile="testajax.aspx.vb" inherits="testajax" %>
2
3<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head id="head1" runat="server">
7 <title>asp.net 2.0 页面提交无刷新演示</title>
8 <script type="text/javascript" language ="javascript">
9 function callserver1()function callserver1(inputcontrol, context)
10 {
11 context.innerhtml = "<img src='http://www.VeVb.com/htmldata/2006-04-03/images/pie.gif' />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 = "<img src='http://www.VeVb.com/htmldata/2006-04-03/images/pie.gif' />数据加载中";
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 </script>
35</head>
36<body>
37 <form id="form1" runat="server">
38 <div>
39 <h1>demo1:html按钮提交数据</h1><br />
40 <asp:textbox id="textbox1" runat="server"></asp:textbox>
41 <input id="button1" type="button" value="提交到label1" />
42 <input id="button2" type="button" value="提交到label2" />
43 <br />
44 <asp:label id="label1" runat="server" text="label1:"></asp:label>
45 <br />
46 <asp:label id="label2" runat="server" text="label2:"></asp:label>
47 </div>
48 <hr />
49 <div>
50 <h1>demo2:服务器按钮提交数据</h1><br />
51 <asp:textbox id="textbox2" runat="server"></asp:textbox>
52 <asp:button id="button3" runat="server" text="button" onclientclick="callserver1(textbox2, label3);return false;" /><br />
53 <asp:label id="label3" runat="server" text="label3:"></asp:label></div>
54 <hr />
55 <div>
56 <h1>demo3:下拉列表框和gridview绑定数据</h1><br />
57 <asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:northwind %>"
58 selectcommand="select distinct(country) from customers"></asp:sqldatasource>
59 <asp:sqldatasource id="sqldatasource2" runat="server" connectionstring="<%$ connectionstrings:northwind %>"
60 selectcommand="select customerid, companyname, country from customers where [email protected]">
61 <selectparameters>
62 <asp:controlparameter name="country" controlid="dropdownlist1" propertyname="selectedvalue" />
63 </selectparameters>
64 </asp:sqldatasource>
65 <div>
66 <asp:dropdownlist id="dropdownlist1" runat="server" width="239px"
67 datasourceid="sqldatasource1" datatextfield="country" datavaluefield="country">
68 </asp:dropdownlist>
69 <input id="button4" type="button" value="刷新" />
70 </div>
71 <br />
72 <span id="gridspan">
73 <asp:gridview id="gridview1" runat="server" datasourceid="sqldatasource2" cellpadding="4" forecolor="#333333" gridlines="none">
74 <footerstyle backcolor="#990000" font-bold="true" forecolor="white" />
75 <rowstyle backcolor="#fffbd6" forecolor="#333333" />
76 <selectedrowstyle backcolor="#ffcc66" font-bold="true" forecolor="navy" />
77 <pagerstyle backcolor="#ffcc66" forecolor="#333333" horizontalalign="center" />
78 <headerstyle backcolor="#990000" font-bold="true" forecolor="white" />
79 <alternatingrowstyle backcolor="white" />
80 </asp:gridview>
81 </span>
82 </div>
83 </form>
84</body>
85</html>
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
77 end class
新闻热点
疑难解答
图片精选