C #. net under the simple Ajax example http://www.diybl.com/2007-12-5 network CLICK: 682 [comment]
-
-
Article Search: [Click to package this article]
[Activate online QQ discussion group on this site]
When I debugged this example yesterday, I asked a Daniel from our company about the usage of native functions at the bottom layer of js. He said that he did not know. I suggest you go to the Internet to find a small framework, it seems that the idea of "never repeating the wheel" has been deeply rooted in the hearts of the people. This suggestion clearly deviates from my intention to write this demo. Of course, google came to the answer.
There may be very few. net programmers hand-writing methods. At the beginning, they were instilled by their predecessors to "use the framework !" , "Use the built-in method of the system !" This concept directly causes us to be relatively weak in some aspects. This is also why it is difficult to write Ajax examples in java, but C # is difficult-it is difficult to find examples without Ajax. dll implementation on the Internet. Now, let's talk about the theme:
We need two pages a. aspx, B. aspx, and a. The Code is as follows (the most common example is to use two drop-down lists to link them ):
<Body>
<Form id = "Form1" method = "post" runat = "server">
<Select id = "AList" onchange = "SetBList ()">
<Option value = "0"> A </option>
<Option value = "1"> B </option>
<Option value = "3"> C </option>
</Select>
<Select id = "BList"> </select>
</Form>
</Body>
After the onchange event is triggered on page a, we enter the SetBList () method written on page a. The complete js is listed here:
<Script language = "javascript">
Var xmlHttp;
Function SetBList ()...{
Var avalue = document. getElementById ("AList"). value;
Var url = "B. aspx? Avalue = "+ avalue;
CreateXMLHttpRequest (); // create an xmlHttp object
XmlHttp. onreadystatechange = handleStateChange; // call the handleStateChage method when the xmlHttp status code changes.
XmlHttp. open ("GET", url, true); // send a request using the GET Method
XmlHttp. send (null );
}
Function BListInitial ()...{
// Clear the BList option first
ClearBList ();
Var blist = document. getElementById ("BList"); // obtain the BList object
Var rs = xmlHttp. responseXML. getElementsByTagName ("City"); // read the data of the <City> tag from the returned xml document.
// The place where the value of this loop is stuck for a while, and the xmlHttp. responseXML is used to create an xml document and read the document. However, the problem is that different browsers use different methods to create xml documents.
For (var I = 0; I <rs. length; I ++ ){
Var option = document. createElement ("OPTION ");
Option. text = rs [I]. getElementsByTagName ("CityName ");
Option. value = rs [I]. getElementsByTagName ("CityCode ");
Blist. options. add (option );
}
}
Function clearBList ()...{
Var ven = document. getElementById ("VendorList ");
While (ven. options. length> 0)
Ven. removeChild (ven. childNodes [0]);
}
Function handleStateChange ()...{
If (xmlHttp. readyState = 4 )...{
If (xmlHttp. status = 200 )...{
BListInitial ();
}
}
}
Function createXMLHttpRequest ()...{
// IE
If (window. ActiveXObject )...{
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
// Mozilla
Else if (window. XMLHttpRequest )...{
XmlHttp = new XMLHttpRequest ();
}
}
</Script>
B. The aspx page deletes all html parts and leaves only one row:
<% @ Page language = "c #" Codebehind = "B. aspx. cs" AutoEventWireup = "false" Inherites = "Test. Ajax" %>
The reason is that we requested the returned xml document on page a. Therefore, we delete the Html Tag section. Then, operate the database in the Page_Load Method on page B, and then write the data in xml format, for example:
//......
// Database operation to obtain the DataTable dt
String xml = "<Data> ";
Foreach (DataRow row in dt. Rows ){
Xml + = "<City> ";
Xml + = "<CityName>" + row ["CityName"] + "</CityName> ";
Xml + = "<CityCode>" + row ["CityCode"] + "</CityCode> ";
Xml + = "</City> ";
}
Xml + = "</Data> ";
// Clear the page format and write xml
Response. ClearContent ();
Response. Cache. SetNoStore ();
Response. ContentType = "text/xml ";
Response. ContentEncoding = System. Text. Encoding. UTF8;
Response. Write (xml );
Before running the entire task, test page B. If the result is similar to the following, it indicates that the data part is correct.
-<Data>
-<Vendor>
<VendorId> 7 </VendorId>
<VendorName> qianqian </VendorName>
</Vendor>
</Data>
At this point, this example has basically ended. If you have any questions, please leave a message in the comment area.
Article Source: http://www.diybl.com/course/4_webprogram/asp.net/asp_netshl/2007125/90639.html