Asp tutorial. net js three-level linkage menu implementation method
Available in ie5.5, ie8, firefox 2.0.0.12, chrome 8.0.552.237
In ashx, obtain the corresponding value from the database tutorial through the input parameters.
Parameter name:
Type: The value is regiontwo (indicating that the second-level region is obtained) or regionthree (indicating that the third-level region is obtained ),
Oneid, indicating the first-level region number,
Twoid, indicating the second-level region number
Getregiontwoinfo and getregionthreeinfo are the methods for obtaining the IDs and names of level 2 and Level 3 regions.
Public void processrequest (httpcontext context)
{
String type = context. request. querystring ["type"];
If (type. equals ("regiontwo "))
{
String oneid = context. request. querystring ["oneid"];
Context. response. contenttype = "text/plain ";
Context. response. write (getregiontwoinfo (oneid ));
}
Else if (type. equals ("regionthree "))
{
String oneid = context. request. querystring ["oneid"];
String twoid = context. request. querystring ["twoid"];
Context. response. contenttype = "text/plain ";
Context. response. write (getregionthreeinfo (oneid, twoid ));
}
}
Js code
Create an xmlhttprequest object, send a request, and set the onreadystatechange function.
Var http_request = false;
Function send_request (url, content, callback)
{
If (window. xmlhttprequest)
{
Http_request = new xmlhttprequest ();
If (http_request.overridemimetype)
{
Http_request.overridemimetype ("text/xml ");
}
}
Else
{
Try
{
Http_request = new activexobject ("msxml2.xmlhttp ");
}
Catch (e)
{
Try
{
Http_request = new activexobject ("microsoft. xmlhttp ");
}
Catch (e)
{
}
}
}
If (! Http_request)
{
Window. alert ("You cannot create an xmlhttprequest object instance. ");
Return false;
}
If (navigator. useragent. indexof ("firefox") <0)
Http_request.onreadystatechange = callback;
Http_request.open ("get", url, false );
Http_request.setrequestheader ("content-type", "text/xml ");
Http_request.send (content );
If (navigator. useragent. indexof ("firefox")> = 0)
{
If (callback = 1)
Http_request.onreadystatechange = populateclass1 ();
Else if (callback = 2)
Http_request.onreadystatechange = populateclass2 ();
}
}
Initialize the drop-down box and call the send_request method. These two functions are called directly in the drop-down box.
Function getregiontwo (oneid)
{
If (oneid! = '0 ')
{
Document. getelementbyid ("region_selregionthree"). options. length = 1;
Var url = "controls/getregionhandler. ashx? Type = regiontwo & oneid = "+ oneid;
If (navigator. useragent. indexof ("firefox") <0)
Send_request (url, null, populateclass1 );
Else
Send_request (url, null, 1 );
}
Else
{
Document. getelementbyid ("region_selregiontwo"). length = 1;
Document. getelementbyid ("region_selregiontwo"). selectedindex = 0;
Document. getelementbyid ("region_selregionthree"). length = 1;
Document. getelementbyid ("region_selregionthree"). selectedindex = 0;
}
}
Function getregionthree (oneid, twoid)
{
If (oneid! = '0' & twoid! = '0 ')
{
Var url = "controls/getregionhandler. ashx? Type = regionthree & oneid = "+ oneid +" & twoid = "+ twoid;
If (navigator. useragent. indexof ("firefox") <0)
Send_request (url, null, populateclass2 );
Else
Send_request (url, null, 2 );
}
Else
{
Document. getelementbyid ("region_selregionthree"). length = 1;
Document. getelementbyid ("region_selregiontwo"). selectedindex = 0;
}
}