In ASP. NET, DropDownList and ListBox implement two-level linkage functions,

Source: Internet
Author: User

In ASP. NET, DropDownList and ListBox implement two-level linkage functions,

DropDownList and ListBox implement two-level linkage functions. They can bind the information searched from the background database. The function to be implemented here is to select "save" in DropDownList ", then let ListBox automatically display the "city" in its province. This is the so-called two-level linkage function, which we can see on many registered webpages, today, we will solve ASP for everyone. NET secret.
1. Set the foreground interface and add the DropDownList and ListBox controls to the Web form.

The following figure shows the interface.

2. compile background code
The background code is written in the Page_Load event of the form.

<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> protected void Page_Load (object sender, EventArgs e) {if (! Page. isPostBack) // determine whether the page is loaded for the first time {SqlConnection con = DB. createConnection (); // This method has been introduced in the previous article, calling a method that has been compiled to create a database connection. SqlCommand cmd = new SqlCommand ("select * from province", con); SqlDataReader sdr = cmd. executeReader (); this. dropDownList1.DataTextField = "proName"; this. dropDownList1.DataValueField = "proID"; // primary key field this. dropDownList1.DataSource = sdr; this. dropDownList1.DataBind (); sdr. close () ;}</span>

Compile the DropDownList1_SelectedIndexChanged Event code, and click "Province". ListBox automatically adds the "city" of the "Province"

<span style="font-family:KaiTi_GB2312;font-size:18px;"> protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)  {   this.ListBox1.Items.Clear();   SqlConnection con2 = DB.createConnection();   SqlCommand cmd1 = new SqlCommand("select * from city where proID=" + this.DropDownList1.SelectedValue, con2);   SqlDataReader sdr1 = cmd1.ExecuteReader();   while (sdr1.Read())   {   this.ListBox1.Items.Add(new ListItem(sdr1.GetString(2),sdr1.GetInt32(0).ToString()));   }  }</span> 

Run the file as follows:

I have not added a complete list of cities in Hebei Province, just to implement the two-level linkage function. Compared with the use of Web controls GridView and Repeater in the previous two articles, although the functions of the GridView and Repeater are quite powerful, different controls have different purposes. How can they be used here?

Articles you may be interested in:
  • Implementation Code of ASP. NET Ajax cascade DropDownList
  • Jquery obtains the value and text value after the HTML tag of the client is generated by the dropdownlist and radiobuttonlist controls on the ASP. NET Server Side.
  • ( c #) Two Methods for displaying corresponding items after DropDownList is bound
  • ASP. NET jQuery instance 7 get the Text/Value Attribute Value of DropDownList through jQuery
  • ASP. NET jQuery instance 17 verifies ListBox by using the jQuery validation plug-in
  • In, multiple options of ListBox binding are selected and deleted.
  • Cause Analysis and Solution for multiple items cannot be selected in DropDownList in

Related Article

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: 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.