asp.net--implementation of two drop-down boxes dynamic linkage

Source: Internet
Author: User

Introduced:

In the Web page, we often encounter the situation in. Select the province in the drop-down box first, and then the second drop-down box will automatically load the city in the province. This design greatly facilitates the user's search. So how does this work?



1. Establish a database

"Province" table


"City" table



2. Adding controls


3, two drop-down boxes bind the data source separately

      protected void Page_Load (object sender, EventArgs e) {//Determines whether to enter the page for the first time, if it is, binds the database, and if not, no binding. if (!this.                IsPostBack) {//bind province SqlConnection con = db.createconnection (); Con.                Open ();                String cmdtext = "select* from province";                SqlCommand cmd = new SqlCommand (Cmdtext, con); SqlDataReader SDR = cmd.                ExecuteReader (); This.                Dropdownlist1.datasource = SDR; This. Dropdownlist1.datatextfield = "Proname";//text content this. Dropdownlist1.datavaluefield = "Proid"; Data source field this.                Dropdownlist1.databind (); Sdr.                Close (); Bound City String cmdcitytext = "select* from town where proid=" + this.                Dropdownlist1.selectedvalue;                SqlCommand cmdcity = new SqlCommand (Cmdcitytext, con);                SDR = Cmdcity.executereader ();                      The remainder is similar to the binding province, slightly         Close connection con.            Close ();   }        }

here, two text boxes have been loaded into their respective data. The rest is the dynamic linkage.

4. When we change the contents of the first drop-down box, the SelectedIndexChanged event of the first text box is triggered. The proid (save ID) of the first drop-down box is used as an argument to find the contents of the city.

The specific code is as follows:

protected void DropDownList1_SelectedIndexChanged (object sender, EventArgs e)        {            //province id            String proid = this . Dropdownlist1.selectedvalue;            SqlConnection con = db.createconnection ();            Con. Open ();            SqlCommand cmd = new SqlCommand ("SELECT * from city where proid=" + proid, con);            SqlDataReader SDR = cmd. ExecuteReader ();            Bind this            . Dropdownlist2.datasource = SDR;            This. Dropdownlist2.datatextfield = "CityName";            This. Dropdownlist2.datavaluefield = "Cityid";            This. Dropdownlist2.databind ();            Sdr. Close ();            Con. Close ();        }


In this way, we can achieve dynamic linkage. Such dynamic linkage, generally consists of a number of drop-down boxes to form a set of menus, such as the above two drop-down boxes used. There is a linkage between the drop-down menus. When a parent's selection changes, the subordinate displays the content according to the selected item in the parent.

Although it is only a small skill or a small demand, but when the amount of data is particularly large, its function can not be overlooked. The last time the final exam guide candidates, it may just be a page ignored this feature, resulting in a significant increase in workload.

With the dynamic linkage, when the face of large data or complex classification, the loading speed of the page will not be affected, but also convenient for users to find.


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.