Provincial and county linkage DropDownList

Source: Internet
Author: User

Here is the code (Code area) to fill in the Click event of the Submit button (provided the database of the provinces and counties is built)

protected void Page_Load (object sender, EventArgs e) {if (!this.       Page.IsPostBack) {getddlprovincedatabind (); Page first load execution province bindings

           }         }            public void Getddlprovincedatabind ()        //Provinces data binding         {             string sqlprovince = "Select * from Province";             Dropdownlist2.datasource = GetDataSet ( Sqlprovince);             Dropdownlist2.datatextfield = "province";             Dropdownlist2.datavaluefield = "Provinceid";             Dropdownlist2.databind ();

            DropDownList2.Items.Insert (0, New ListItem ("-- Provinces--"," 0 "));        }         protected void Dropdownlist2_selectedindexchanged (object sender, EventArgs e)         {            //First floor, provinces select events              {                 int provinceid = Convert.ToInt32 (Dropdownlist2.selectedvalue);                 if (Provinceid > 0)                 {                     string sqlcity = "SELECT * from the cityWHERE father= "+ Provinceid +" ";      //Find City       by province ID                Dropdownlist3.datasource = GetDataSet (sqlcity);                      Dropdownlist3.datatextfield = "City";                      Dropdownlist3.datavaluefield = "Cityid";                      Dropdownlist3.databind ();

                     DropDownList3.Items.Insert (0, New ListItem ("--Please select City-", "0"));                }                  Else                  {                     DropDownList3.Items.Clear ();                      DropDownList3.Items.Insert (0, New ListItem ("--Please select City-", "0"));                      DropDownList3.Items.Clear ();                     DropDownList3.Items.Insert (0, New ListItem ("--Please select County--", "0"));                }             }        }

        protected void Dropdownlist3_selectedindexchanged (object sender, EventArgs e)         {            //Second floor, city pieces             {                 int Cityid = Convert.ToInt32 ( Dropdownlist3.selectedvalue);                 if (Cityid > 0)                 {                     string sqldistrict = "SELECT * from area  WHERE father=" + Cityid + "";      //Find county by City ID &nbs p;                   Dropdownlist4.datasource = GetDataSet (sqldistrict);                      Dropdownlist4.datatextfield = "area";                      Dropdownlist4.datavaluefield = "Areaid";                      Dropdownlist4.databind ();

                     DropDownList4.Items.Insert (0, New ListItem ("--Please select County--", "0"));                }                  Else                  {                     DropDownList4.Items.Clear ();                      DropDownList4.Items.Insert (0, New ListItem ("--Please select County--", "0"));                }             }        }

        protected void Dropdownlist4_selectedindexchanged (object sender, EventArgs e)         {             int provinceid = Convert.ToInt32 (Dropdownlist2.selectedvalue);             int Cityid = Convert.ToInt32 ( Dropdownlist3.selectedvalue);             int districtid = Convert.ToInt32 ( Dropdownlist4.selectedvalue);            //if (Provinceid > 0 && cityid > 0 && districtid > 0)            //{            //    Response.Write ("The Province ID you selected:" + Provinceid + "City ID:" + Cityid + "County ID:" + districtid + "");            /}        }         Public DataSet GetDataSet (String sql)       //custom method, SQL statement parameter, return dataset DataSet          {            String connection = configurationmanager.connectionstrings["Sqlcon"]. ConnectionString;             SqlConnection conn = new SqlConnection ( connection);             SqlDataAdapter SDA = new SqlDataAdapter (SQL, Conn );             DataSet ds = new DataSet ();             SDA. Fill (DS);             return DS;        }

In this way, we will finish the desired effect. As shown in the following:

Provincial and county linkage DropDownList

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.