A no refresh two-level linkage drop-down list, the same applies to Firefox, this is Ajax Mody?

Source: Internet
Author: User
Tags add format empty flush net string split tostring
ajax| Refresh | no Refresh | dropdown | drop down list

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 so his state is not saved, do not know this is Ajax?
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 "%>



WebForm1








runat= "Server" >
Motorola
Nokia

runat= "Server" >
text= "button" >




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;

A summary description of the

namespace drop
{
 ///


 ///WebForm1.
 ///
 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 the user code here to initialize the page
   //if (! IsPostBack)
   //{
    binddrop ();//This cannot be placed in the IF (! IsPostBack) inside the
   //}
  

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 ()));
}
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);
}


Designer supports required methods-do not use the Code editor to modify
The contents of this method.

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 ());
}
}
}

Data table:
Primary key ID parent_id (int) type_value (int) type_text (varchar)
int increments the value of the parent Drop-down box value drop-down box text



Related Article

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.