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 ();
}
}
---------------------------------
Take a closer look at ''In fact, there is no need to sacrifice a N-1 page in response to multiple droplist, because all responses have a common, why can't I pass another value to determine the response method ???
VaR type = "type1 ";
Ohttpreq. Open ("Post", "webform2.aspx? State = "+ state +" & type = "+ type, false );
String type = This. request ["type"]. tostring ();
Switch (type)
{
Case "type1 ":
.........................................
}
----------------------------------------------
The program needs to make several modifications:
1. Solve the unclear and incomplete problems of drp2 and drp3: drp2.innertext = ""; drp3 .......................
2,
For (VAR item = items. nextnode (); item = items. nextnode ())
{
VaR city = item. nodetypedvalue;
VaR newoption = Document. createelement ("option ");
Newoption. Text = city;
Newoption. value = city;
Drp2.options. Add (newoption );
}
3. The same is true if multiple values are passed''
Select T1, T2 from TT
You only need to add one more condition to solve the problem.
========================================================== ====================
There are still many questions that can be studied, such as the value after refreshing ............
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 548121
Demo address: http://www.718.com.cn/other/Example.aspx
I saw some new linkage controls on the Internet. After the test, English and numbers are displayed, but Chinese characters are not displayed. Later, I made a flip-over modification to the files that have been down from the Internet and made a linkage control for provinces and cities. Share it with you. Chinese characters are displayed.
<% @ Page Language = "C #" codebehind = "example. aspx. cs" autoeventwireup = "false" inherits = "webs. Other. Example" %>
<HTML>
<Head>
<Title> example </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "code_language" content = "C #">
<Meta name = "vs_defaultclientscript" content = "JavaScript">
<Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5">
<SCRIPT>
Function load (state ){
VaR drp2 = Document. getelementbyid ("dropdownlist2 ");
For (I = drp2.length; I> = 0; I --){
Drp2.options. Remove (I );
}
VaR ohttpreq = new activexobject ("msxml2.xmlhttp ");
VaR odoc = new activexobject ("msxml2.domdocument ");
Ohttpreq. Open ("Post", "getdata. aspx? State = "+ state, false );
Ohttpreq. Send ("");
Result = ohttpreq. responsetext;
Odoc. loadxml (result );
Items1 = odoc. selectnodes ("// city/table/ID ");
Items2 = odoc. selectnodes ("// city/table/shiname ");
VaR itemslength = items1.length;
For (I = 0; I <itemslength; I ++)
// Assign the class name and number of the sub-class to dropdownlist2
{
VaR newoption = Document. createelement ("option ");
Newoption. Text = items2 [I]. text;
Newoption. value = items1 [I]. text;
Drp2.options. Add (newoption );
}
}
Window. onload = function () {load ('1 ');}
</SCRIPT>
</Head>
<Body ms_positioning = "flowlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Asp: dropdownlist id = "dropdownlist1" runat = "server"> </ASP: dropdownlist>
<Asp: dropdownlist id = "dropdownlist2" runat = "server"> </ASP: dropdownlist>
<Asp: textbox id = "th" runat = "server"> </ASP: textbox>
<Asp: button id = "button1" runat = "server" text = "button"> </ASP: button>
</Form>
</Body>
</Html>
Cs source file:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Namespace webs. Other
{
/// <Summary>
/// Summary of example.
/// </Summary>
Public Class Example: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. dropdownlist dropdownlist1;
Protected system. Web. UI. webcontrols. textbox th;
Protected system. Web. UI. webcontrols. Button button1;
Protected system. Web. UI. webcontrols. dropdownlist dropdownlist2;
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
If (! This. ispostback)
{
// Create a data source to load the first dropdownlist. You can also load the second one by default.
Sqlconnection con = new sqlconnection (system. configuration. configurationsettings. deleettings. Get ("connstr1 "));
Sqldataadapter da = new sqldataadapter ("select ID, shengname from Province", con );
Dataset DS = new dataset ();
Da. Fill (DS );
This. dropdownlist1.datasource = Ds;
This. dropdownlist1.datatextfield = "shengname ";
This. dropdownlist1.datavaluefield = "ID ";
This. dropdownlist1.databind ();
// Here is the binding client event. When the first dropdownlist option changes, the following event onchange is triggered. This event will call a client method load ()
This. dropdownlist1.attributes. Add ("onchange", "load (this. Options [This. selectedindex]. Value )");
}
}
# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Private void button#click (Object sender, system. eventargs E)
{
Th. Text = This. Request. Form ["dropdownlist2"]. tostring ();
}
}
}
Getdata source file
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Using system. xml;
Using system. configuration;
Using system. text;
Namespace webs. Other
{
/// <Summary>
/// Summary of getdata.
/// </Summary>
Public class getdata: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
// Response. Write (request ["state"]);
Int shengno = int. parse (request ["state"]. tostring ());
Sqlconnection con = new sqlconnection (system. configuration. configurationsettings. deleettings. Get ("connstr1 "));
Sqldataadapter da = new sqldataadapter ("select ID, shiname from city where shengid = '" + shengno + "'", con );
Dataset DS = new dataset ("city ");
Da. Fill (DS );
Xmltextwriter writer = new xmltextwriter (response. outputstream, encoding. utf8 );
Writer. Formatting = formatting. indented;
Writer. indentation = 4;
Writer. indentchar = '';
Writer. writestartdocument ();
DS. writexml (writer );
Writer. Flush ();
Response. End ();
Writer. Close ();
}
# Region web form designer generated code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 472917
Asp.net dropdownlist: no refreshing! <Title> webform2 </title>
<Script language = 'javascript '>
Function load (){
VaR drp2 = Document. getelementbyid ("dropdownlist2 ");
Drp2.innertext = "";
For (VAR I = 0; I <= drp2.options. Length-1; I ++ ){
Drp2.remove (I );
}
VaR drp3 = Document. getelementbyid ("dropdownlist3 ");
For (VAR I = 0; I <= drp3.options. Length-1; I ++ ){
Drp3.remove (I );
}
VaR XMLHTTP = new activexobject ("msxml2.xmlhttp ");
VaR odoc = new activexobject ("msxml2.domdocument ");
VaR state = Document. getelementbyid ("dropdownlist1"). value;
XMLHTTP. Open ("Post", "webform1.aspx? Id = "+ state, false );
XMLHTTP. Send ("");
VaR res = odoc. loadxml (XMLHTTP. responsetext );
VaR naitems = odoc. selectnodes ("// market/table/maname ");
VaR iditems = odoc. selectnodes ("// market/table/maid ");
VaR item;
VaR ID;
For (item = naitems. nextnode (), id = iditems. nextnode (); item & ID; item = naitems. nextnode (), id = iditems. nextnode ()){
VaR nastr = item. nodetypedvalue;
VaR idstr = ID. nodetypedvalue;
VaR newoption = Document. createelement ("option ");
Newoption. Text = nastr;
Newoption. value = idstr;
Drp2.options. Add (newoption );
}
Load2 ();
}
Function load2 (){
VaR drp2 = Document. getelementbyid ("dropdownlist3 ");
Drp2.innertext = "";
For (VAR I = 0; I <= drp2.options. Length-1; I ++ ){
Drp2.remove (I );
}
VaR XMLHTTP = new activexobject ("msxml2.xmlhttp ");
VaR odoc = new activexobject ("msxml2.domdocument ");
VaR state = Document. getelementbyid ("dropdownlist2"). value;
XMLHTTP. Open ("Post", "webform3.aspx? Id = "+ state, false );
XMLHTTP. Send ("");
VaR res = odoc. loadxml (XMLHTTP. responsetext );
VaR naitems = odoc. selectnodes ("// market/table/maname ");
VaR iditems = odoc. selectnodes ("// market/table/maid ");
VaR item;
VaR ID;
For (item = naitems. nextnode (), id = iditems. nextnode (); item & ID; item = naitems. nextnode (), id = iditems. nextnode ()){
VaR nastr = item. nodetypedvalue;
VaR idstr = ID. nodetypedvalue;
VaR newoption = Document. createelement ("option ");
Newoption. Text = nastr;
Newoption. value = idstr;
Drp2.options. Add (newoption );
}
}
</SCRIPT>
</Head>
<Body ms_positioning = "gridlayout" onLoad = "load ()">
<Form ID = "form1" method = "Post" runat = "server">
<Asp: dropdownlist id = "dropdownlist1" runat = "server"> </ASP: dropdownlist>
<Asp: dropdownlist id = "dropdownlist2" runat = "server"> </ASP: dropdownlist>
<Asp: dropdownlist id = "dropdownlist3" runat = "server"> </ASP: dropdownlist>
</Form>
</Body>
</Html>
-----------------------
Namespace dropdown
{
/// <Summary>
/// Summary of webform2.
/// </Summary>
Public class webform2: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. dropdownlist dropdownlist1;
Protected system. Web. UI. webcontrols. dropdownlist dropdownlist3;
Protected system. Web. UI. webcontrols. dropdownlist dropdownlist2;
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
If (! This. ispostback)
{
Sqlconnection con = new sqlconnection ("Server = localhost; database = star; uid = sa; Pwd = ");
Sqldataadapter da = new sqldataadapter ("select maid, maname from tabmarket where macls = 0", con );
Dataset DS = new dataset ();
Da. Fill (DS, "op ");
This. dropdownlist1.datasource = Ds. Tables [0];
This. dropdownlist1.datatextfield = "maname ";
This. dropdownlist1.datavaluefield = "maid ";
This. dropdownlist1.databind ();
This. dropdownlist1.attributes. Add ("onchange", "load ()");
This. dropdownlist2.attributes. Add ("onchange", "load2 ()");
}
}
}
}
-----------------------
Public class webform1: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
// Put user code to initialize the page here
// If (this. request ["state"]! = NULL)
//{
String id = This. request ["ID"];
Sqlconnection con = new sqlconnection ("Server = localhost; database = star; uid = sa; Pwd = ;");
Sqldataadapter da = new sqldataadapter ("select maname, maid from tabmarket where maparent = @ ID and mast = 1", con );
Da. selectcommand. Parameters. Add ("@ ID", ID );
Dataset DS = new dataset ("market ");
Da. Fill (DS );
DS. writexml (response. outputstream );
Response. Flush ();
Response. End ();
//}
}
}
-----------------------------
Public class webform3: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
String id = request ["ID"];
Sqlconnection con = new sqlconnection ("Server = localhost; database = star; uid = sa; Pwd = ;");
Sqldataadapter da = new sqldataadapter ("select maname, maid from tabmarket where maparent = @ ID and mast = 1", con );
Da. selectcommand. Parameters. Add ("@ ID", ID );
Dataset DS = new dataset ("market ");
Da. Fill (DS );
DS. writexml (response. outputstream );
Response. Flush ();
Response. End ();
}
}
------------------
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1377536