Webform Three-level linkage example

Source: Internet
Author: User

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

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.