Front-endCodeThe implementation of AJAX is as follows:
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
$ (Initmenuinfo );
Function initmenuinfo ()
{
VaR tbody = "";
$. Ajax ({
Type: "Get ",
Datatype: "JSON ",
URL: 'tree. aspx ',
Data: "s = 1 ",
Error: function (){
Tbody = "data loading failed ";
$ ("# Menu"). append (tbody );
},
Success: function (data ){
VaR menus = data. menumodels;
$. Each (menus, function (I, menu ){
If (menu! = NULL)
{
VaR divstr = "Div" + menu. moduleid;
VaR TRS = "";
TRS + = '<Div id = "' + divstr + '"> ';
TRS + = '<a href = "#" class = "menuover" Title = "' + menu. modulename + '" onclick = "Node
('+ Menu. moduleid +'); "> ';
TRS + = menu. modulename + '</a> ';
TRS + = '</div> ';
Tbody + = TRS;
}
});
If (tbody = "")
Tbody = "no data ";
$ ("# Menu"). addclass ("hasload"). append (tbody );
},
Complete: function (XMLHttpRequest, textstatus ){
}
});
}
Function node (ID ){
VaR divid = "Div" + id. tostring ();
VaR tbody1 = "";
If (! $ ("#" + Divid + ""). hasclass ("hasload "))
{
$. Ajax ({
Type: "Get ",
Datatype: "JSON ",
URL: 'tree. aspx ',
Data: "F = 2 & id =" + id,
Error: function (){
Tbody1 = "data loading failed ";
$ ("#" + Divid + ""). append (tbody1 );
},
Success: function (data ){
VaR menus = data. menumodels;
$. Each (menus, function (I, menu ){
If (menu! = NULL)
{
VaR divstr = "Div" + menu. moduleid;
VaR trs1 = "";
Trs1 + = '<Div class = "menuover" id = "' + divstr + '"> ';
Trs1 + = '<a href = "#" Title = "' + menu. modulename + '" onclick = "Node
('+ Menu. moduleid +'); "> ';
Trs1 + = menu. modulename + '</a> ';
Trs1 + = '</div> ';
Tbody1 + = trs1;
}
});
If (tbody1! = "")
$ ("#" + Divid + ""). addclass ("hasload"). append (tbody1 );
},
Complete: function (XMLHttpRequest, textstatus ){
}
});
}
Else
{
$ ("#" + Divid + "> Div"). Toggle ();
$ ("#" + Divid + "> div> Div"). Hide ();
// If (document. getelementbyid (divid). getelementsbytagname ("Div") [0]. classname = "menuover ")
//{
// Alert ("AA ");
// For (j = 0; j <document. getelementbyid (divid). getelementsbytagname ("Div"). length; j ++)
//{
// Var OBJ = Document. getelementbyid (divid). getelementsbytagname ("Div") [J];
// Obj. classname = obj. classname. Replace ("menuover", "menuhide ");
//}
//}
// Else
//{
// For (j = 0; j <document. getelementbyid (divid). getelementsbytagname ("Div"). length; j ++)
//{
// Var OBJ = Document. getelementbyid (divid). getelementsbytagname ("Div") [J];
// Obj. classname = obj. classname. Replace ("menuhide", "menuover ");
//}
//}
}
}
</SCRIPT>
<Div id = "menu">
</Div>
The background code is as follows: mainly to build a JSON object
Protected void page_load (Object sender, eventargs E)
{
If (request. querystring ["S"] = "1 ")
{
Getfirstmenu ();
}
If (request. querystring ["F"] = "2 ")
{
Int nodeid = int. parse (request. querystring ["ID"]);
Getlastmenu (nodeid );
}
}
Public void getfirstmenu ()
{
Dataset DS = new dataset ();
String strsql = "select * From menu where parentid = 0 ";
DS = dbhelper. Query (strsql );
If (Ds! = NULL)
{
List <menumodel> menumodels = new list <menumodel> ();
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Menumodel = new menumodel ();
Menumodel. moduleid = int. parse (Ds. Tables [0]. Rows [I] ["moduleid"]. tostring ());
Menumodel. modulename = Ds. Tables [0]. Rows [I] ["modulename"]. tostring ();
Menumodel. parentid = int. parse (Ds. Tables [0]. Rows [I] ["parentid"]. tostring ());
Menumodel. ispage = bool. parse (Ds. Tables [0]. Rows [I] ["ispage"]. tostring ());
Menumodel. urladdress = Ds. Tables [0]. Rows [I] ["urladdress"]. tostring ();
Menumodels. Add (menumodel );
}
String jsonstr = getjsonstr (menumodels );
Response. Write (jsonstr );
Response. End ();
}
}
Public void getlastmenu (int id)
{
Dataset DS = new dataset ();
String strsql = "select * From menu where parentid = '" + ID + "'";
DS = dbhelper. Query (strsql );
If (Ds! = NULL)
{
List <menumodel> menumodels = new list <menumodel> ();
For (INT I = 0; I <Ds. Tables [0]. Rows. Count; I ++)
{
Menumodel = new menumodel ();
Menumodel. moduleid = int. parse (Ds. Tables [0]. Rows [I] ["moduleid"]. tostring ());
Menumodel. modulename = Ds. Tables [0]. Rows [I] ["modulename"]. tostring ();
Menumodel. parentid = int. parse (Ds. Tables [0]. Rows [I] ["parentid"]. tostring ());
Menumodel. ispage = bool. parse (Ds. Tables [0]. Rows [I] ["ispage"]. tostring ());
Menumodel. urladdress = Ds. Tables [0]. Rows [I] ["urladdress"]. tostring ();
Menumodels. Add (menumodel );
}
string jsonstr = getjsonstr (menumodels);
response. write (jsonstr);
response. end ();
}< BR >}< br> Public String getjsonstr (list menumodels)
{< br> stringbuilder JSON = new stringbuilder ();
JSON. append ("{\" menumodels \ ": [");
foreach (menumodel model in menumodels)
{< br> JSON. append ("{\" moduleid \ ": \" "+ model. moduleid + "\", \ "modulename \": \ "" + model. modulename +
"\", \ "Parentid \": \ "" + model. parentid + "\", \ "ispage \": \ "" + model. ispage + "\", \ "urladdress \": \ "" +
Model. urladdress + "\"}");
JSON. append (",");
}
Return JSON. tostring (). trimend (',') + "]}";
}
Public class menumodel
{
Public int moduleid {Get; set ;}
Public String modulename {Get; set ;}
Public int parentid {Get; set ;}
Public String urladdress {Get; set ;}
Public bool ispage {Get; set ;}
}