First step: Setting up the interface
1<%@ Control language="C #"autoeventwireup="true"codefile="PCAControl.ascx.cs"inherits="Pcacontrol"%>2<asp:scriptmanager id="ScriptManager1"runat="Server">3</asp:ScriptManager>4<asp:updatepanel id="UpdatePanel1"runat="Server">5<ContentTemplate>6<asp:dropdownlist id="Ddlprovice"runat="Server"appenddatabounditems="true"autopostback="true" 7Onselectedindexchanged="ddlprovice_selectedindexchanged">8<asp:listitem text="-Please select province-"Value=""></asp:ListItem>9</asp:DropDownList>Ten<asp:dropdownlist id="ddlcity"runat="Server"autopostback="true" OneOnselectedindexchanged="ddlcity_selectedindexchanged"> A<asp:listitem text="-Please select CITY-"Value=""></asp:ListItem> -</asp:DropDownList> -<asp:dropdownlist id="Ddlarea"runat="Server"> the<asp:listitem text="-Please select County District-"Value=""></asp:ListItem> -</asp:DropDownList> -</ContentTemplate> -</asp:UpdatePanel>
Step two: C # write method functions
1 usingSystem.Collections.Generic;2 usingSystem.Linq;3 usingsystem.web;4 usingSystem.Data;5 usingSystem.Web.UI;6 usingSystem.Web.UI.WebControls;7 8 Public Partial classPCAControl:System.Web.UI.UserControl9 { Ten One protected voidPage_Load (Objectsender, EventArgs e) A { - if(!IsPostBack) - { the bindprovince (); - } - } - + //Loading Provinces - protected voidbindprovince () + { ADataTable dt = Maticsoft.DBUtility.DbHelperOra.Query ("SELECT DISTINCT Province from Sy_citycode GROUP by province"). tables[0]; atDdlprovice.datatextfield ="Province"; -Ddlprovice.datavaluefield ="Province"; -Ddlprovice.datasource =DT; - Ddlprovice.databind (); - } - in //Load City - protected voidDdlprovice_selectedindexchanged (Objectsender, EventArgs e) to { + stringSelectpro =Ddlprovice.selectedvalue; - if(!string. IsNullOrEmpty (Selectpro)) the { * ddlCity.Items.Clear (); $Ddlcity.appenddatabounditems =true; Panax NotoginsengDdlCity.Items.Insert (0,NewListItem ("-Please select CITY-","")); -DataTable dt1 = Maticsoft.DBUtility.DbHelperOra.Query ("SELECT City from Sy_citycode Where province= '"+ Selectpro +"' Group by City '"). tables[0]; theDdlcity.datatextfield =" City"; +Ddlcity.datavaluefield =" City"; ADdlcity.datasource =dt1; the Ddlcity.databind (); + } - } $ $ //Load County Area - protected voidDdlcity_selectedindexchanged (Objectsender, EventArgs e) - { the stringSelectcity =Ddlcity.selectedvalue; - if(!string. IsNullOrEmpty (selectcity))Wuyi { the ddlArea.Items.Clear (); -Ddlarea.appenddatabounditems =true; WuDdlArea.Items.Insert (0,NewListItem ("-Please select County District-","")); -DataTable DT2 = Maticsoft.DBUtility.DbHelperOra.Query ("SELECT area from Sy_citycode Where city= '"+ selectcity +"'"). tables[0]; AboutDdlarea.datatextfield =" Area"; $Ddlarea.datavaluefield =" Area"; -Ddlarea.datasource =DT2; - Ddlarea.databind (); - } A } +}
Step three: Call the function in the page
1 protected voidButton1_Click (Objectsender, EventArgs e)2 { 3 stringSelectedprov = ((DropDownList) Pcacontrol1.findcontrol ("Ddlprovice")). SelectedValue; 4 stringSelectedcity = ((DropDownList) Pcacontrol1.findcontrol ("ddlcity")). SelectedValue; 5 stringSelectedarea = ((DropDownList) Pcacontrol1.findcontrol ("Ddlarea")). SelectedValue; 6MscL. Jshelper.alert (Selectedprov +"-"+ selectedcity +"-"+Selectedarea, Page); 7}
02-26c# three-level provinces and cities Ajax linkage controls, using UpdatePanel, and page values