A simple Ajax example
Last Update:2018-12-06
Source: Internet
Author: User
(1) Ajax. JS File Code
Function doplstajaxchenged ()
{
VaR myajax = Window. form1;
Getnames (myajax. doplstajax. value );
}
Function getnames (doplstid)
{
Requesturl = "../ajaxfrontend/doplstid. aspx? Doplstid = ";
VaR url = requesturl + doplstid;
XMLHTTP = createxmlhttpobject (updatelabel );
Xmlhttpget (XMLHTTP, URL );
}
Function updatelabel ()
{
If (XMLHTTP. readystate = 4 | XMLHTTP. readystate = 'complete ')
{
VaR STR = XMLHTTP. responsetext;
Document. getelementbyid ('lblaja'). innertext = STR;
}
}
Function createxmlhttpobject (funtioncall)
{
VaR objxmlhttp = NULL;
VaR strobjname = "Microsoft. XMLHTTP ";
Objxmlhttp = new activexobject (strobjname)
Objxmlhttp. onreadystatechange = funtioncall;
Return objxmlhttp;
}
Function xmlhttpget (httpobj, URL)
{
Httpobj. Open ('get', URL, true );
Httpobj. Send (null );
}
(2) Ajax. xml file code
<? XML version = "1.0" encoding = "UTF-8"?>
<Namestore>
<Matename>
<Name> select a name </Name>
<Value>-1 </value>
</Matename>
<Matename>
<Name> sun Lu </Name>
<Value> 0 </value>
</Matename>
<Matename>
<Name> Song Yu </Name>
<Value> 1 </value>
</Matename>
<Matename>
<Name> Wang Qing </Name>
<Value> 2 </value>
</Matename>
</Namestore>
(3) A base class testajax. CS code.
Public static string floorresident (INT floorid)
{
String names = string. empty;
Switch (floorid)
{
Case 0:
Names = "My beautiful beacher! ";
Break;
Case 1:
Names = "a beautiful gril! ";
Break;
Case 2:
Names = "a beautiful gril, too! ";
Break;
Default:
Names = "Please select a name! ";
Break;
}
Return names;
}
(4) HTML page content of the main form
<% @ Page Language = "C #" codebehind = "testajax. aspx. cs" autoeventwireup = "false" inherits = "ajaxfrontend. testajax" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> testajax </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "code_language" content = "C #">
<Meta name = "vs_defaultclientscript" content = "JavaScript">
<Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5">
<Script language = "JavaScript" src = Ajax. js> </SCRIPT>
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Asp: dropdownlist id = "doplstajax" style = "Z-INDEX: 101; left: 328px; position: absolute; top: 168px"
Runat = "server" onchange = "doplstajaxchenged ()">
</ASP: dropdownlist>
<Asp: Label id = "lblajax" style = "Z-INDEX: 102; left: 328px; position: absolute; top: 216px" runat = "server"> label </ASP: label>
</Form>
</Body>
</Html>
The. CS Page code of the main form.
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Dataset DS = new dataset ();
String xmlpath = server. mappath ("Ajax. xml ");
DS. readxml (xmlpath );
This. doplstajax. datasource = Ds. Tables [0];
This. doplstajax. datatextfield = "name ";
This. doplstajax. datavaluefield = "value ";
This. doplstajax. databind ();
}
The CS Page code of the subform.
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Int doplstid = 0;
Try
{
If (request. querystring ["doplstid"]! = NULL)
{
Doplstid = convert. toint32 (request. querystring ["doplstid"]);
}
}
Catch
{
Doplstid = 0;
}
String result = testajax. doplstnames (doplstid );
Response. Write (result );
}