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