Let's talk about N Asp.net dropdownlist brushless New Linkage menus, sacrifice N-1 aspx to achieve automatic update mechanism, to avoid refreshing flashing !!
The following code can be used, or you can implement a multi-level menu ....
<Script language = "JavaScript">
Function load (state)
{
VaR drp2 = Document. getelementbyid ("dropdownlist2 ");
For (VAR I = 0; I <= drp2.options. Length-1; I ++)
{
Drp2.remove (I );
}
VaR ohttpreq = new activexobject ("msxml2.xmlhttp"); // send a request
VaR odoc = new activexobject ("msxml2.domdocument"); // response result
VaR state = Document. getelementbyid ("dropdownlist1"). value;
Ohttpreq. Open ("Post", "webform2.aspx? State = "+ state, false );
Ohttpreq. Send ("");
Result = ohttpreq. responsetext;
Odoc. loadxml (result );
// Items = odoc. selectnodes ("// city/table ");
Items = odoc. selectnodes ("// address/table ");
For (VAR item = items. nextnode (); item = items. nextnode ())
{
VaR city = item. selectsinglenode ("// address"). nodetypedvalue;
VaR newoption = Document. createelement ("option ");
Newoption. Text = city;
Newoption. value = city;
Drp2.options. Add (newoption );
}
}
</SCRIPT>
======================================
Webform1.aspx
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
If (! Ispostback)
{
Sqlconnection con = new sqlconnection ("packet size = 4096; user id = sa; Data Source = server; persist Security info = false; initial catalog = pubs ");
Sqldataadapter da = new sqldataadapter ("select state from authors group by State", con );
Dataset DS = new dataset ("state ");
Da. Fill (DS );
This. dropdownlist1.datasource = Ds. Tables [0];
This. dropdownlist1.datatextfield = "state ";
This. dropdownlist1.datavaluefield = "state ";
This. dropdownlist1.databind ();
This. dropdownlist1.attributes. Add ("onchange", "load ()");
// Dropdownlist1.
}
}
Webform2.aspx
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
If (this. request ["state"]! = NULL)
{
String state = This. request ["state"]. tostring ();
Sqlconnection con = new sqlconnection ("packet size = 4096; user id = sa; Data Source = server; persist Security info = false; initial catalog = pubs ");
Sqldataadapter da = new sqldataadapter ("select address from authors where State = '" + state + "'", con );
Dataset DS = new dataset ("Address ");
Da. Fill (DS );
Xmltextwriter writer = new xmltextwriter (response. outputstream, response. contentencoding );
Writer. Formatting = formatting. indented;
Writer. indentation = 4;
Writer. indentchar = '';
DS. writexml (writer );
Writer. Flush ();
Response. End ();
Writer. Close ();
}
}