<% @ Page Language = "C #" autoeventwireup = "true" codefile = "casmenu. aspx. cs" inherits = "_ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> porschev -- foreground JS (jquery) calls the cascading menu of the background method </title>
<SCRIPT src = "http://www.cnblogs.com/Javascript/jquery-1.4.1.js" type = "text/JavaScript"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
$ (Function (){
$ ("# Selectshop"). Change (function () {// change event of the province drop-down menu
VaR Params = '{STR: "' + $ (this). Val () + '"}'; // The parameter name must be consistent with the parameter name of the background method.
$. Ajax ({
Type: "Post", // submission method
URL: "list. aspx/showshoppro", // submitted page/method name
Data: Params, // parameter (if no parameter exists: NULL)
Datatype: "text", // type
Contenttype: "application/JSON; charset = UTF-8 ",
Beforesend: function (XMLHttpRequest ){
$ ('# Tipsdiv'). Text ("querying ...");
},
Success: function (MSG ){
$ ('# Tipsdiv'). Text ("query successful! ");
$ ("# Selectshoppro option"). Each (function (){
$ (This). Remove (); // remove the original item
});
$ ("<Option value = '0' >== select ===</option>"). appendto ("# selectshoppro"); // Add a default item
$ (Eval ("(" + MSG + ")"). d). appendto ("# selectshoppro"); // Add the returned items to the drop-down list
},
Error: function (xhr, MSG, e ){
Alert ("error ");
}
});
});
});
VaR I = 1;
// Clone and add
Function adds (){
$ ("# Selectshop"). Clone (). prependto ("# tipsdiv ");
$ ("# Tipsdiv select"). ATTR ("ID", "select" + I + "");
Alert ($ ("# tipsdiv" pai.html ());
I ++;
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
Porschev -- foreground JS (jquery) calls the cascading menu of the background method <br/>
<Br/>
<Select id = "selectshop">
<% = Strshop %>
</SELECT>
<Select id = "selectshoppro" name = "selcity">
<Option value = "0" >== select ==</option>
</SELECT>
<Div id = "tipsdiv"> </div>
<A href = "javascript: adds ()"> Add </a>
</Div>
</Form>
</Body>
</Html>
Casmenu. aspx. CS File
using system;
using system. collections. generic;
using system. LINQ;
using system. web;
using system. web. ui;
using system. web. UI. webcontrols;
using casmenumodels;
using casmenubll;
using system. text;
Public partial class _ default: system. web. UI. page
{< br> protected void page_load (Object sender, eventargs e)
{< br> If (! Ispostback)
{< br> showpro ();
}< BR >}
Public static string strshop = string. empty;
Public static string strshoppro = string. empty;
Public String showpro ()
{
Stringbuilder STR = new stringbuilder ();
List <zgwed. model. Shop> List = new shopbll (). getallitems ();
// Add an initial entry
Str. append ("<option value = \"");
Str. append ("0 ");
Str. append ("\"> ");
Str. append ("= select = ");
Str. append ("</option> ");
// Cyclically append a province drop-down item
Foreach (zgwed. model. Shop S in List)
{
Str. append ("<option value = \"");
Str. append (S. ID );
Str. append ("\"> ");
Str. append (S. shopname );
Str. append ("</option> ");
}
Return strshop = Str. tostring ();
}
[System. Web. Services. webmethod ()]
Public static string showshoppro (string Str)
{
Stringbuilder strci = new stringbuilder ();
If (STR = "0") // is the initial item.
{
Strci. append ("<option value = \"");
Strci. append ("select ");
Strci. append ("\"> ");
Strci. append ("select ");
Strci. append ("</option> ");
}
Else
{
List <string> scon = new list <string> ();
Scon. Add ("shopid," + STR + ", = ");
List <zgwed. model. shopproductinfo> List = new shopproductinfobll (). getitemsbycondition (scon );
Foreach (zgwed. model. shopproductinfo C in List)
{
Strci. append ("<option value = \"");
Strci. append (C. ID );
Strci. append ("\"> ");
Strci. append (C. shopid );
Strci. append ("</option> ");
}
}
Return strshoppro = strci. tostring ();
}
# Endregion
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/porschev/archive/2010/10/15/5943579.aspx