Asp.net three-level linkage

Source: Internet
Author: User

There are a lot of online connections, and the method is also good. But this is especially true for me because the Organization has already established a database and you have to add it on it. So it's very troublesome. I also found some on the Internet, but most databases are different. Some of them are made using vs2005 and ajaxpro. However, my company is using vs2003 and I can only find some available ones. Here, I would like to thank Eric for his good method in Baidu's blog. I basically used it, but I made some changes here.
First. Threeddl. ASPX page Program Write it all up,
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>

<Script language = "JavaScript">
<! --
Function selectprovince ()
{
VaR drp2 = Document. getelementbyid ("dropdownlist2"); // city/county level 1
VaR drp3 = Document. getelementbyid ("dropdownlist3"); // zone-level
For (VAR I = drp2.options. length; I> = 0; I --)
{
Drp2.remove (I );
}
For (VAR I = drp3.options. length; I> = 0; I --)
{
Drp3.remove (I );
}
VaR ohttpreq = new activexobject ("msxml2.xmlhttp ");
VaR odoc = new activexobject ("msxml2.domdocument ");
VaR province = Document. getelementbyid ("dropdownlist1"). value;
Ohttpreq. Open ("Post", "selectarea. aspx? Flag = 1 & province = "+ province, false );
Ohttpreq. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
Ohttpreq. Send ("");
VaR result = ohttpreq. responsetext;
VaR newoption_0 = Document. createelement ("option ");
Newoption_0.text = "-select -";
Newoption_0.value = '-1 ';
Drp2.options. Add (newoption_0 );
Odoc. loadxml (result );
// Here, items1 and items2 are an XML table you get in the subsequent program. For how the // newdataset/city/name comes from, you can use alert (result); just take a look.
Items1 = odoc. selectnodes ("// newdataset/city/Name ");
Items2 = odoc. selectnodes ("// newdataset/city/sz_code ");

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 = items1 [I]. text;
Newoption. value = items2 [I]. text;
Drp2.options. Add (newoption );
}
}


Function selectcity ()
{
VaR drp3 = Document. getelementbyid ("dropdownlist3 ");
For (VAR I = drp3.options. length; I> = 0; I --)
{
Drp3.remove (I );
}

VaR ohttpreq = new activexobject ("msxml2.xmlhttp ");
VaR odoc = new activexobject ("msxml2.domdocument ");
VaR city = Document. getelementbyid ("dropdownlist2"). value;
Ohttpreq. Open ("Post", "selectarea. aspx? Flag = 2 & City = "+ city, false );
Ohttpreq. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
Ohttpreq. Send ("");
VaR result = ohttpreq. responsetext;
VaR newoption_0 = Document. createelement ("option ");
Newoption_0.text = "-select -";
Newoption_0.value = '-1 ';
Drp3.options. Add (newoption_0 );
Odoc. loadxml (result );
VaR items = odoc. selectnodes ("// newdataset/area ");
For (VAR item = items. nextnode (); item = items. nextnode ())
{

VaR areaname = item. selectsinglenode ("name"). nodetypedvalue;
VaR areacode = item. selectsinglenode ("sz_code"). nodetypedvalue;
VaR newoption = Document. createelement ("option ");
Newoption. Text = areaname;
Newoption. value = areacode;
Drp3.options. Add (newoption );
}
}


-->
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: dropdownlist id = "dropdownlist1" runat = "server">
</ASP: dropdownlist>
<Asp: dropdownlist id = "dropdownlist2" runat = "server">
</ASP: dropdownlist>
<Asp: dropdownlist id = "dropdownlist3" runat = "server">
</ASP: dropdownlist> </div>
</Form>
</Body>
</Html>
Below is the threeddl. aspx. CS Code
Protected void page_load (Object sender, eventargs E)
{
Sqlconnection conn = new sqlconnection ("Data Source = CG; initial catalog = area; user id = sa; Password = chen123 ");

Sqldataadapter da = new sqldataadapter ("select * from area where right (sz_code, 4) = '000000'", Conn );
Dataset DS = new dataset ();

Da. Fill (DS, "Province ");
This. dropdownlist1.datasource = Ds. Tables ["Province"];
This. dropdownlist1.datatextfield = "name ";
This. dropdownlist1.datavaluefield = "sz_code ";
This. dropdownlist1.databind ();

This. dropdownlist1.attributes. Add ("onchange", "selectprovince ()");
This. dropdownlist2.attributes. Add ("onchange", "selectcity ()");
}

selectarea. there is nothing on the page of aspx, only in its selectarea. aspx. the Code is available in CS.
using system;
using system. data;
using system. data. sqlclient;
using system. configuration;
using system. collections;
using system. web;
using system. web. security;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. webcontrols. webparts;
using system. web. UI. htmlcontrols;
using system. XML;

Public partial class selectarea: system. web. UI. page
{< br> protected void page_load (Object sender, eventargs e)
{< br> sqlconnection conn = new sqlconnection ("Data Source = CG; initial catalog = area; user id = sa; Password = chen123 ");
int flag = convert. toint32 (request. querystring ["flag"]. tostring ();
If (request. querystring ["flag"]! = NULL | request. querystring ["flag"]. tostring ()! = "")
{< br> switch (FLAG)
{< br> case 1:

String strprv = request. querystring ["Province"]. tostring ();
Sqldataadapter da = new sqldataadapter ("select * from area where left (sz_code, 2) = '" + strprv. substring (0, 2 ). tostring () + "'and right (sz_code, 2) = '00' and right (sz_code, 4) <> '123'", Conn );
Dataset DS = new dataset ();

Da. Fill (DS, "city ");
Xmltextwriter writercity = new xmltextwriter (response. outputstream, response. contentencoding );
Writercity. Formatting = formatting. indented;
Writercity. indentation = 4;
Writercity. indentchar = '';
DS. writexml (writercity );
Writercity. Flush ();
Response. End ();
Writercity. Close ();
Break;
Case 2:
String strcity = request. querystring ["city"]. tostring ();
Sqldataadapter da1 = new sqldataadapter ("select * from area where left (sz_code, 4) = '" + strcity. substring (0, 4 ). tostring () + "'and right (sz_code, 2) <> '00'", Conn );
Dataset ds1 = new dataset ();

Da1.fill (ds1, "area ");
Xmltextwriter writercity1 = new xmltextwriter (response. outputstream, response. contentencoding );
Writercity1.formatting = formatting. indented;
Writercity1.indentation = 4;
Writercity1.indentchar = '';
Ds1.writexml (writercity1 );
Writercity1.flush ();
Response. End ();
Writercity1.close ();
Break;
}
}
}
}

Here I will give the general situation of the database to the family, if you want to database ftp: // down: down@ftp.netc.hutc.zj.cn/city.rar this is a friend of Eric download I do not know how long can be used, my database is similar to it, because I cannot provide my database, so the above Code is written according to its database format.
Table name Area
ID name Rome sz_code zm_code
1 Beijing Shi 110000 BJ

Here, the province-level independent cities such as Beijing and Tianjin, sz_code are the first two, and the last four are all 0000.
At the lower level, the first two are the same as those at the provincial level. The second is different from the second in the middle, and the second is 00.
At the last level and at the district level, the first two are the same as the provincial level, the middle is the same as the municipal level, and the last two are their own numbers.

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.