Step 1: Return XML strings from the database Step 1: Return XML strings from the database # Region Step 1: Return XML strings from the database
Public Static String Getxml ( Int Parentid)
{
Ilist < Categoryinfo > Listctgs = New Category (). getcategories (parentid );
String XML = " <Ctgs> " ;
For ( Int I = 0 ; I < Listctgs. Count; I ++ )
{
XML + = " <CTG> " ;
XML + = " <Name> " + Replacexml (listctgs [I]. categoryname) + " </Name> " ;
XML + = " <NO> " + Listctgs [I]. categoryno + " </NO> " ;
XML + = " <Haschild> " + Listctgs [I]. haschild. tostring () + " </Haschild> " ;
XML + = " <Namepath> " + Replacexml (listctgs [I]. namepath) + " </Namepath> " ;
XML + = " </CTG> " ;
}
XML + = " </Ctgs> " ;
Return XML;
}
# Endregion
Step 2: Create a new category. asmx and return the xmlwebmethod XML document. Step 2: Create a new category. asmx and return the xmlwebmethod XML document. # Region Step 2: Create a new category. asmx and return the xmlwebmethod XML document.
[Webmethod]
Public Xmldatadocument getcategoriesxml ( Int Categoryno)
{
Xmldatadocument XD= NewXmldatadocument ();
XD. loadxml (getxml (categoryno ));
ReturnXD;
}
# Endregion
Step 3 Write a common jquery Ajax return XML method with a callback function Step 3 Write a common jquery Ajax return XML method with a callback function # Region Step 3 Write a common jquery Ajax return XML method with a callback function
Function xmlwebservice (URL, pars, callback) {
$. Ajax ( {
Data: pars,
URL: URL,
Type: " Post " ,
Contenttype: " Text/XML; UTF-8 " , // UTF-8 is required.
Datatype: ' XML ' ,
Cache: True ,
Success: function (data) {
Callback (data );
} ,
Error: function (data, status) {
Alert ('Ajax xmlwebservicer error \ n'+Data. responsetext );
}
}
);
}
# Endregion
Step 4: Read the nodes in XML to the control on the Web Front-end. Step 4: Read the nodes in XML to the control on the Web Front-end. # Region Step 4: Read the nodes in XML to the control on the Web Front-end.
$ (Document). Ready (function () {
Initcurpage ();
} );
// Initial page
Function initcurpage ()
{
Readlevelctgs (1,1);
}
// List of subclass XML format
Function readlevelctgs (levelnum, parentid)
{
// Jquery Ajax value and callback
Xmlwebservice ( ' Category. asmx/getcategoriesxml ' , {Categoryno: parentid} , Function (data) {Buildlevelctgs (levelnum, data )} );
}
// Bind the read XML file to the front-end control.
Function buildlevelctgs (levelnum, XML)
{
$ (Curdivid). Text ( "" );
$ (XML). Find ( ' CTG ' ). Each (function () {
VaR ctgdiv=$ ('<Div> </div>');
Ctgdiv. appendto (curdivid );
} );
}
# Endregion
// Author: Keen
// Note: jquery AJAX can return
From: http://www.cnblogs.com/xiaobaigang/archive/2008/04/03/1135984.html