asp.net| Example | refresh | no Refresh | page
Please see Leon.zhou in the original:http://pfzhou.cnblogs.com/archive/2006/03/31/363342.html
Download the sample code, and converted to VB, Ajax functional Test success. But there are some minor problems:
Demo1 is normal.
Demo2 according to Leon original wording TestAJAX.aspx.vb line 22 unsuccessful. So I directly modify the Button3 OnClientClick attribute in the IDE environment, see Line 52 of testajax.asp. The test was successful.
Demo3 failed to succeed with the original join client property, and 20th row did not work in TestAJAX.aspx.vb. I don't know what the reason is. So I have to add a button3 to raise the client event.
This is a good example, it is worth studying, detailed principle explanation please see author's original text. When testing, add <add name= "NorthWind connectionstring=" in the web.config of your project ... Corresponding database connection string ... ">
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
5http://www.w3.org/1999/xhtml">
67 <title>asp.net 2.0 page Submit no refresh demo </title>
8 <script type= "text/javascript" language = "JavaScript" >
9 function CallServer1 () function CallServer1 (Inputcontrol, context)
10 {
One context.innerhtml = "loading";
arg = ' servermethod1| ' + inputcontrol.value;
<%= clientscript.getcallbackeventreference (Me, "Arg", "ReceiveServerData1", "context")%>;
14}
15
function ReceiveServerData1 () function ReceiveServerData1 (result, context)
17 {
context.innerhtml = context.id + ":" + result;
19}
20
function CallServer2 () function CallServer2 (obj)
22 {
context = Gridspan;
context.innerhtml = " Data loading";
arg = "servermethod2|" + obj.value;
<%= clientscript.getcallbackeventreference (Me, "Arg", "ReceiveServerData2", "context")%>;
27}
28
The function ReceiveServerData2 () function ReceiveServerData2 (result, context)
30 {
context.innerhtml = result;
32}
33
</script>
3536<body>
Notoginseng <form id= "Form1" runat= "Server" >
<div>
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
<input id= "Button1" type= "button" value= "Submit to Label1"/>
<input id= "Button2" type= "button" value= "Submit to Label2"/>
<br/>
<asp:label id= "Label1" runat= "Server" text= "Label1:" ></asp:Label>
<br/>
<asp:label id= "Label2" runat= "Server" text= "Label2:" ></asp:Label>
</div>
<div>
Wuyi <asp:textbox id= "TextBox2" runat= "Server" ></asp:TextBox>
<asp:button id= "Button3" runat= "Server" text= "button" onclientclick= "CallServer1 (TextBox2, LABEL3); return FAL se; "/><br/>
<asp:label id= "Label3" runat= "Server" text= "LABEL3:" ></asp:Label></div>
<div>
<asp:sqldatasource id= "SqlDataSource1" runat= "connectionstring=" <%$ connectionstrings:northwind%& gt; "
selectcommand= "SELECT DISTINCT (country) from customers" ></asp:SqlDataSource>
<asp:sqldatasource id= "SqlDataSource2" runat= "connectionstring=" <%$ connectionstrings:northwind%& gt; "
Selectcommand= "SELECT CustomerID, CompanyName, country from customers wherecountry= @Country">
<SelectParameters>
<asp:controlparameter name= "Country" controlid= "DropDownList1" propertyname= "SelectedValue"/>
</SelectParameters>
</asp:SqlDataSource>
<div>
<asp:dropdownlist id= "DropDownList1" runat= "Server" width= "239px"
Datasourceid= "SqlDataSource1" datatextfield= "Country" datavaluefield= "Country" >
</asp:DropDownList>
<input id= "Button4" type= "button" value= "Refresh"/>
</div>
<br/>
<span id= "Gridspan" >
<asp:gridview id= "GridView1" runat= "Server" datasourceid= "SqlDataSource2" cellpadding= "4" forecolor= "#333 333 "gridlines=" None ">
<footerstyle backcolor= "#990000" font-bold= "True" forecolor= "white"/>
<rowstyle backcolor= "#FFFBD6" forecolor= "#333333"/>
<selectedrowstyle backcolor= "#FFCC66" font-bold= "True" forecolor= "Navy"/>
<pagerstyle backcolor= "#FFCC66" forecolor= "#333333" horizontalalign= "Center"/>
<alternatingrowstyle backcolor= "White"/>
</asp:GridView>
Bayi </span>
</div>
</form>
84</body>
8586
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
Inherits System.Web.UI.Page
Implements ICallbackEventHandler
Protected Sub Page_Load () Sub Page_Load (ByVal sender as Object, ByVal e as EventArgs)
18 ' Registered client event handling method
19 ' It doesn't seem to work, so I'm not adding a button4 to raise the client event by Andydavis
DROPDOWNLIST1.ATTRIBUTES.ADD ("onchange", "CallServer2 (This)")
The ' Button3 client event can be set directly in the OnClientClick property in the IDE and does not need to be added here by Andydavis
BUTTON3.ATTRIBUTES.ADD ("onclick", "CallServer1 (TextBox2, LABEL3); return false;")
End Sub
24
Serverreturn Private as String
26
The Public Function GetCallbackResult () function GetCallbackResult () as String Implements icallbackeventhandler.getcallb Ackresult
28 ' for ease of viewing load effect, add delay
System.Threading.Thread.Sleep (2000)
30
Dim parts () as String = Serverreturn.split ("|") C
32 ' calls according to the method name passed and passes the corresponding parameter, only one parameter is currently supported
A-return CStr ([GetType] (). GetMethod (Parts (0)). Invoke (Me, New Object () {parts (1)}))
The End Function
35
36
Notoginseng public Sub raisecallbackevent () Sub RaiseCallbackEvent (ByVal eventargument as String) Implements Icallbackeventhandle R.raisecallbackevent
Serverreturn = eventargument
The End Sub
40
41 ' According to the English country name or abbreviation from the client, translated into the corresponding Chinese country name
The Public Function ServerMethod1 () function ServerMethod1 (ByVal arg as String) as String
Dim s as String = ""
Select case Arg. ToLower ()
Case "CN"
Case "a"
s = "China"
+ Exit Select
Case "US"
s = "United States"
Wuyi Exit Select
Case Else
s = "Unknown Country"
The Exit Select
The End Select
return s
End Function
58
59 ' Updates the contents of the GridView according to the value from the client, and returns the HTML of the updated GridView
The Public Function ServerMethod2 () function ServerMethod2 (ByVal arg as String) as String
Dropdownlist1.selectedvalue = arg
Gridview1.databind ()
63
RenderControl (GridView1)
The End Function
66
Private Overloads function RenderControl () function RenderControl (ByVal control as control) as String
Dim writer1 as StringWriter = New StringWriter (cultureinfo.invariantculture)
Writer2 Dim as HtmlTextWriter = New HtmlTextWriter (writer1)
70
Control. RenderControl (Writer2)
Writer2. Flush ()
Writer2. Close ()
74
Return Writer1. ToString ()
The End Function
The End Class