There may be a lot of criticism, but I think this is really good. I have read about 20 refresh dynamic drop-down lists, and they are a mess under firefox. for this I spent almost two days, that is, how to keep the value of the second list box after submitting the form, because an entry is added to the drop-down box through js, its status will not be saved on the test platform: ie6, firefox
Function: second-level brushless New Linkage
Features: cross-browser; submit a form to get the value of the second drop-down box; data comes from the database; send requests with xmlhttp to achieve no refreshing
Request: If you can find a better method, please let me know. Thank you very much. Your criticism and suggestions are a great encouragement to me.
Webform1.aspx:
<% @ Page language = "c #" Codebehind = "WebForm1.aspx. cs" AutoEventWireup = "false" Inherits = "drop. WebForm1" %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<Title> WebForm1 </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 language = "javascript">
// The jb function initializes xmlhttp Objects Based on Different browsers.
Function jb ()
{
Var A = null;
Try
{
A = new ActiveXObject ("Msxml2.XMLHTTP ");
}
Catch (e)
{
Try
{
A = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Catch (oc)
{
A = null
}
}
If (! A & typeof XMLHttpRequest! = "Undefined ")
{
A = new XMLHttpRequest ()
}
Return
}
// The following Go function is called when the parent list box is changed. The parameter is the selected entry.
Function Go (obj)
{
// Obtain the value of the drop-down list in the selection box.
Var svalue = obj. value;
// Define the page for Data Processing
Var weburl = "webform1.aspx? Parent_id = "+ svalue;
// Initialize xmlhttp objects
Var xmlhttp = jb ();
// Submit data. The first parameter is preferably get, and the third parameter is preferably true.
Xmlhttp. open ("get", weburl, true );
// Alert (xmlhttp. responseText );
// If data is successfully returned
Xmlhttp. onreadystatechange = function ()
{
If (xmlhttp. readyState = 4) // 4 indicates that data is returned successfully.
{
Var result = xmlhttp. responseText; // get the data returned by the server
// Clear all dListChild drop-down items first
Document. getElementById ("dListChild"). length = 0;
// Add all models to dListChild. Note that Option is not option.
Document. getElementById ("dListChild"). options. add (new Option ("all models", "0 "));
If (result! = "") // If the returned data is not empty
{
// Divide the received string into arrays according
Var allArray = result. split (",");
// Loop this array, note that it starts from 1, because the first character of the received string is, number, so the first array after segmentation is empty
For (var I = 1; I <allArray. length; I ++)
{
// Split the string into an array according to |
Var thisArray = allArray [I]. split ("| ");
// Add entries for dListChild
Document. getElementById ("dListChild"). options. add (new Option (thisArray [1]. toString (), thisArray [0]. toString ()));
}
}
}
}
// Send data. Pay attention to the sequence and parameters. The parameter must be null or ""
Xmlhttp. send (null );
}
</Script>
</HEAD>
<Body MS_POSITIONING = "GridLayout">
<Form id = "Form1" method = "post" runat = "server">
<Asp: DropDownList onchange = "Go (this)" id = "dListParent" style = "Z-INDEX: 101; LEFT: 248px; POSITION: absolute; TOP: 40px"
Runat = "server">
<Asp: ListItem Value = "100"> Motorola </asp: ListItem>
<Asp: ListItem Value = "101"> Nokia </asp: ListItem>
</Asp: DropDownList>
<Asp: DropDownList id = "dListChild" style = "Z-INDEX: 102; LEFT: 248px; POSITION: absolute; TOP: 104px"
Runat = "server"> </asp: DropDownList>
<Asp: Button id = "Button1" style = "Z-INDEX: 103; LEFT: 256px; POSITION: absolute; TOP: 176px" runat = "server"
Text = "Button"> </asp: Button>
</Form>
</Body>
</HTML>
Backend webform1.aspx. cs:
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. Configuration;
Using System. Data. SqlClient;
Namespace drop
{
/// <Summary>
/// Summary of WebForm1.
/// </Summary>
Public class WebForm1: System. Web. UI. Page
{
Protected System. Web. UI. WebControls. DropDownList dListParent;
Protected System. Web. UI. WebControls. Button Button1;
Protected System. Web. UI. WebControls. DropDownList dListChild;
Private void Page_Load (object sender, System. EventArgs e)
{
// Place user code here to initialize the page
// If (! IsPostBack)
//{
BindDrop (); // bind the list box if it is not submitted back.
//}
}
Protected void BindDrop ()
{
// First, I want to bind the parent dropdownlist to the database, which is unnecessary.
// If (! IsPostBack)
//{
// Bind the parent dListParent
// BindParent ();
//}
// Obtain the passed parent_id value. If it is the first request, it is null.
String str = Request. QueryString ["parent_id"];
String str1 = dListParent. SelectedValue ;;
Response. Write (str1 );
// If str is added with a string! = The original string indicates that the onchange event of dListParent was triggered.
If (str + "abc ")! = "Abc ")
{
// Bind the dListChild Control
BindChild (str); // use the value of the parent DropDownList as the parameter.
}
Else
BindParent (str1 );
}
Protected void BindParent (string str)
{
// If this is the first request or the page is refreshed, select the value of dListParent.
// Convert the parameter to int
Int I = Convert. ToInt32 (str );
DListChild. Items. Clear ();
DListChild. Items. Add (new ListItem ("all models", "0 "));
// Obtain the database connection string
String connStr = ConfigurationSettings. receivettings ["ConnString"]. ToString ();
// Initialize conn objects
SqlConnection conn = new SqlConnection (connStr );
// Database statement
String commStr = string. Format ("select type_value, type_text from phone_type where parent_id = {0}", I );
// Create a database command object
SqlCommand comm = new SqlCommand (commStr, conn );
// Open the database
Conn. Open ();
// Execute the command
SqlDataReader dr = comm. ExecuteReader ();
// Cyclically add entries to the dListParent
While (dr. Read ())
{
DListChild. Items. Add (new ListItem (dr [1]. ToString (), dr [0]. ToString ()));
// You can also
// DListParent. Items. Add (new ListItem (dr ["phone_text"]. ToString (), dr ["phone_value"]. ToString ()));
}
DListParent. Items [0]. Selected = true;
// Add the following statement to save the status of the second dListChild when the click submit button submits the form.
DListChild. SelectedValue = Request. Form ["dListChild"];
Dr. Close ();
Conn. Close ();
}
Protected void BindChild (string str)
{
// The content added to any controls including dropdownlist through js will not be saved
// Convert the parameter to int
Int I = Convert. ToInt32 (str );
// Define strings to save the data returned from the database
String result = "";
// Clear the output first
Response. Clear ();
String connStr = ConfigurationSettings. receivettings ["ConnString"]. ToString ();
SqlConnection conn = new SqlConnection (connStr );
SqlCommand comm = conn. CreateCommand ();
String commStr = string. Format ("select type_value, type_text from phone_type where parent_id = {0}", I );
Comm. CommandText = commStr;
Conn. Open ();
SqlDataReader dr = comm. ExecuteReader ();
While (dr. Read ())
{
Result + = "," + dr [0]. ToString () + "|" + dr [1]. ToString ();
// DListChild. Items. Add (new ListItem (dr [1]. ToString (), dr [0]. ToString ()));
}
// Output the information obtained from the database to the client
Response. Write (result );
// Close Response after the output is complete to avoid unnecessary output
Response. Flush ();
Response. Close ();
Dr. Close ();
Conn. Close ();
}
# Code generated by region Web Form Designer
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)
{
Response. Write (Request. Form ["dListChild"]. ToString ());
}
}
}