ASP. NET data binding-DropDownList, ListBox,

Source: Internet
Author: User

ASP. NET data binding-DropDownList, ListBox,

DropDownList and ListBox implement two-level linkage functions. They can also 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. We can see this function on many registered webpages. Today we use ASP.. NET.

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 to the use of Web controls GridView and Repeater in the previous two blogs, although the functions of the GridView and Repeater are quite powerful, different controls have different purposes. How can they be used here?


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.