First, make three drop-down lists, respectively.
<body> <form id="Form1"runat="Server"> <asp:dropdownlist id="DropDownList1"runat="Server"autopostback="True"></asp:DropDownList> <asp:dropdownlist id="DropDownList2"runat="Server"autopostback="True"></asp:DropDownList> <asp:dropdownlist id="DropDownList3"runat="Server"></asp:DropDownList></form></body>
Building class: China entity class
PublicChina () {}Private stringCode; Public stringCode {Get{returnCode;} Set{Code =value;} } Private stringname; Public stringName {Get{returnname;} Set{name =value;} } Private stringPrentcode; Public stringPrentcode {Get{returnPrentcode;} Set{Prentcode =value;} }
Data Access classes:
Public classchinadata{SqlConnection Conn=NULL; SqlCommand cmd=NULL; PublicChinadata () {conn=NewSqlConnection ("server=.; Database=mydb;user=sa;pwd=123"); CMD=Conn. CreateCommand (); } PublicList<china> Select (stringPcode) {Cmd.commandtext="SELECT * from chinastates where [email protected]"; Cmd. Parameters.clear (); Cmd. Parameters.addwithvalue ("@pcode", Pcode); Conn. Open (); SqlDataReader Dr=cmd. ExecuteReader (); List<China> list =NewList<china>(); if(Dr. HasRows) { while(Dr. Read ()) {China data=NewChina (); Data. Code= dr[0]. ToString (); Data. Name= dr[1]. ToString (); Data. Prentcode= dr[2]. ToString (); List. ADD (data);
}
}
Conn. Close ();
return list;
}
}
. CS Background Code
First Data binding:
if(!IsPostBack) { //calling method binding DataBind (DropDownList1,NewChinastatesdata (). Select ("0001")); Bind (DropDownList2,NewChinastatesdata (). Select (Dropdownlist1.selectedvalue)); Bind (DropDownList3,NewChinastatesdata (). Select (Dropdownlist2.selectedvalue)); }//DelegateDropdownlist1.selectedindexchanged + =dropdownlist1_selectedindexchanged; Dropdownlist2.selectedindexchanged+ = dropdownlist2_selectedindexchanged;
// method of Creation: Private void Bind (DropDownList DDL, list<chinastates> list) { = list; " AreaName " ; " AreaCode " ; Ddl. DataBind (); }
//SelectedIndexChanged Events voidDropDownList1_SelectedIndexChanged (Objectsender, EventArgs e) {Bind (DropDownList2,NewChinastatesdata (). Select (Dropdownlist1.selectedvalue));//filled CityBind (DROPDOWNLIST3,NewChinastatesdata (). Select (Dropdownlist2.selectedvalue));//Fill Area }voidDropdownlist2_selectedindexchanged (Objectsender, EventArgs e) {Bind (DROPDOWNLIST3,NewChinastatesdata (). Select (Dropdownlist2.selectedvalue));//Fill area}
When the code section is complete, change the AutoPostBack property of the drop-down list to True
Webform Three-level linkage example