Apply with Firefox ASP. NET no refresh two-level linkage Drop-down List _ practical skills

Source: Internet
Author: User
Perhaps "excellent" will bring a lot of criticism, but I think it's really good, and I've seen about 20 flash-free drop-down lists, and they're a mess underneath Firefox. I've been doing this for two days, and that's how if you submit a form, you can keep the value of the second list box, because you pass the JS Add an entry to the Drop-down box then his state is not to be saved test platform: Ie6,firefox

Function: Two-level non-flush connection

Features: cross-browser, submit the form to take the value of the second Drop-down box, data from the database, XMLHTTP to send the request, the implementation of no refresh

Request: If you can find a better way please tell me, thank you very much, your criticism and suggestion is 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 a XMLHTTP object based on a different browser
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 A
}

The following go function is called when the parent list box changes, and the argument is the selected entry
function Go (obj)
{
Gets the value of the Drop-down list for the selection box
var svalue = Obj.value;
Define the page to process data
var weburl = "webform1.aspx?parent_id=" +svalue;
Initializing a XMLHTTP Object
var xmlhttp = JB ();
Submit data, the first parameter is better get, the third argument best is True
Xmlhttp.open ("Get", weburl,true);
alert (Xmlhttp.responsetext);
If the data has been successfully returned
Xmlhttp.onreadystatechange=function ()
{
if (xmlhttp.readystate==4)//4 represents a successful return of data
{
var result = xmlhttp.responsetext;//Gets the data returned by the server
Empty the Dlistchild all the dropdown items first
document.getElementById ("Dlistchild"). length = 0;
Add a full model 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 groups by
var Allarray = Result.split (",");
Loop This array, note that starting from 1, since the first character of the received string is the number, so the first array after the split is empty
for (Var i=1;i<allarray.length;i++)
{
Divide the array by the |
var thisarray = allarray[i].split ("|");
Add an entry for Dlistchild
document.getElementById ("Dlistchild"). Options.add (New Option (Thisarray[1].tostring (), thisarray[0].tostring ());
}
}
}
}
To send data, note the order and parameters, the parameters 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= > Motorola </asp:ListItem>
<asp:listitem value= "A" > 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>

Backstage 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 description of the 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 page
if (! IsPostBack)
//{
Binddrop ()//if not submitted back on binding list box
//}
}

protected void Binddrop ()
{
First I want the parent DropDownList to bind the database too.
if (! IsPostBack)
//{
Bind parent Dlistparent
Bindparent ();
//}
Get the parent_id value passed over, and if it's the first time he's asked for null
String str = request.querystring["parent_id"];
string str1 = Dlistparent.selectedvalue;;
Response.Write (STR1);
If Str adds a string!= the original string indicates the onchange event that triggered the Dlistparent
if ((str+ "abc")!= "abc")
{
Binding Dlistchild Controls
Bindchild (str);//The value of the Father DropDownList is the argument
}
Else
Bindparent (STR1);
}


protected void Bindparent (String str)
{
If this is the first request or refresh of this page, it is based on the value of Dlistparent.
Converts a parameter to an int
int i = Convert.ToInt32 (str);
DListChild.Items.Clear ();
DLISTCHILD.ITEMS.ADD (New ListItem ("All Models", "0"));
Get the database connection string
String connstr = configurationsettings.appsettings["ConnString"]. ToString ();
Initializing a Conn Object
SqlConnection conn = new SqlConnection (CONNSTR);
Database statements
String commstr = String. Format ("Select Type_value,type_text from Phone_type where parent_id={0}", i);
To establish a database command object
SqlCommand comm = new SqlCommand (commstr,conn);
Open Database
Conn. Open ();
Execute command
SqlDataReader dr = Comm. ExecuteReader ();
Loop Dr, add entries to Dlistparent
while (Dr. Read ())
{
DLISTCHILD.ITEMS.ADD (New ListItem (dr[1). ToString (), dr[0]. ToString ()));
I can do that.
DLISTPARENT.ITEMS.ADD (New ListItem (dr["Phone_text"). ToString (), dr["Phone_value"]. ToString ()));
}
Dlistparent.items[0]. Selected = true;
Adding the following means that the state of the second dlistchild can be saved when the Point submit button submits the form
Dlistchild.selectedvalue = request.form["Dlistchild"];
Dr. Close ();
Conn. Close ();
}


protected void Bindchild (String str)
{
Content added to any control including DropDownList through JS will not be saved state
Converts a parameter to an int
int i = Convert.ToInt32 (str);
Define a string to save data returned from the database
string result = "";
Empty the output first.
Response.Clear ();
String connstr = configurationsettings.appsettings["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 information from the database to the client
Response.Write (Result);
The output finishes closing the response so as not to cause unnecessary output
Response.Flush ();
Response.close ();
Dr. Close ();
Conn. Close ();
}
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

private void Button1_Click (object sender, System.EventArgs e)
{
Response.Write (request.form["Dlistchild"). ToString ());
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.