Without refreshing new provinces, cities, and counties to jointly update JavaScript + C # + SQL Server

Source: Internet
Author: User

HTML code

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<SCRIPT src = "Ajax. js" type = "text/JavaScript"> </SCRIPT>
<Title> No title page </title>
<SCRIPT type = "text/JavaScript">
VaR XMLHTTP;
Function createxmlhttprequest ()
{
If (window. activexobject)
{
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest)
{
XMLHTTP = new XMLHttpRequest ();
}
}
Function changeprovince (j)
{
Equality ();
Createxmlhttprequest ();
VaR url = "handler. ashx? PID = "+ J +" & flag = 0 ";
XMLHTTP. Open ("get", URL, true );
XMLHTTP. onreadystatechange = showresult;
XMLHTTP. Send (null );
}
Function showresult ()
{
If (XMLHTTP. readystate = 4)
{
If (XMLHTTP. Status = 200)
{
VaR content = XMLHTTP. responsetext;
Content = content. substring (0, content. Length-1 );

VaR arrtmp = content. Split (",");
VaR myele;
If (arrtmp. length> 0)
{
For (VAR I = 0; I <arrtmp. length; I ++)
{
Myele = Document. createelement ("option ");
Myele. value = arrtmp [I]. tostring ();
I + = 1;
Myele. Text = arrtmp [I]. tostring ();
Form1.city. Add (myele );
}
Form1.text1. value = "loaded ";
}
}
Else
{
Alert (XMLHTTP. status );
}
}
Else
{
Form1.text1. value = "loading ...";
}
}
Function equality ()
{
VaR city = Document. form1.city;
For (VAR I = city. Options. length; I> = 0; I --)
{
City. Options [I] = NULL;
}
VaR area = Document. form1.area;
For (var y = area. Options. length; y> = 0; y --)
{
Area. Options [y] = NULL;
}
}
Function cleararea ()
{
VaR area = Document. form1.area;
For (var y = area. Options. length; y> 0; y --)
{
Area. Options [y] = NULL;
}
}
Function citychange ()
{
// Alert (A. Options [A. Options. selectedindex]. Value); // obtain the index
// Alert (A. Options [A. Options. selectedindex]. Text); // obtain the selected text value
Cleararea ();
Createxmlhttprequest ();
VaR url = "handler. ashx? PID = "+ A +" & flag = 1 ";
// Alert (URL );
XMLHTTP. Open ("get", URL, true );
XMLHTTP. onreadystatechange = showarea;
XMLHTTP. Send (null );
}
Function showarea ()
{
If (XMLHTTP. readystate = 4)
{
If (XMLHTTP. Status = 200)
{
VaR content = XMLHTTP. responsetext;
Content = content. substring (0, content. Length-1 );
// Alert (content );
VaR arrtmp = content. Split (",");
VaR myele;
If (arrtmp. length> 0)
{
For (VAR I = 0; I <arrtmp. length; I ++)
{
Myele = Document. createelement ("option ");
Myele. value = arrtmp [I]. tostring ();
I + = 1;
Myele. Text = arrtmp [I]. tostring ();
Form1.area. Add (myele );
}
Form1.text1. value = "loaded ";
}
}
Else
{
// Alert ('test ');
Alert (XMLHTTP. status );
}
}
Else
{
Form1.text1. value = "loading ...";
}
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: Label id = "label1" runat = "server" text = "Province"> </ASP: Label>
<Asp: dropdownlist id = "Province" runat = "server" width = "123px">
</ASP: dropdownlist>
<Asp: Label id = "label2" runat = "server" text = ""> </ASP: Label> & nbsp;
<Select id = "city" style = "width: 122px" onchange = "citychange (this. Options [This. Options. selectedindex]. Value)">
</SELECT>
<Asp: Label id = "label3" runat = "server" text = "zone"> </ASP: Label>
<Select id = "area" style = "width: 141px">
</SELECT>
<Input id = "text1" type = "text"/> </div>
</Form>
</Body>
</Html>

Handler. ashxCode

<% @ Webhandler Language = "C #" class = "handler" %>

Using system;
Using system. Web;
Using system. Data. sqlclient;
Using system. Data. SQL;

Public class handler: ihttphandler {

Public void processrequest (httpcontext context)
{
Context. response. contenttype = "text/plain ";
String c = system. configuration. configurationsettings. configurettings ["con"];
Sqlconnection con = new sqlconnection (C );
Con. open ();
If (context. Request. querystring ["flag"]. tostring () = "0 ")
{
String pid = context. Request. querystring ["PID"];
String content = "";
String SQL = "select * from city where Father =" + PID;
Sqlcommand COM = new sqlcommand (SQL, con );
Sqldatareader rs = com. executereader ();
While (Rs. Read ())
{
Content + = Rs ["cityid"] + "," + Rs ["city"] + ",";
}
Context. response. Write (content );

}
If (context. Request. querystring ["flag"]. tostring () = "1 ")
{
String pid = context. Request. querystring ["PID"];
String content = "";
String SQL = "select * from area where Father =" + PID;
Sqlcommand COM = new sqlcommand (SQL, con );
Sqldatareader rs = com. executereader ();
While (Rs. Read ())
{
Content + = Rs ["areaid"] + "," + Rs ["area"] + ",";
}
Context. response. Write (content );
}
Con. Close ();
}
 
Public bool isreusable {
Get {
Return false;
}
}
}

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.