Association between Asp.net-DropDownList and listbox

Source: Internet
Author: User

In the asp.net control, some controls support database binding, such as dropdownlist, listbox, checkboxgroup, and radiobuttnlist. These controls bind a database to display the corresponding data.

In the actual project, there will be such a requirement. For example, after the dropdownlist is bound to a database, select the corresponding content and the specific entries under the corresponding content will be displayed in the listbox. For example, if you bind all provinces to the database in the dropdownlist, the city of the corresponding province will appear in listbox after you select a province. This is the two-level linkage.

The following describes the two controls used (the attributes and events used in code implementation ):

Introduction to the dropdownlist Control

The dopdownlist control is used to create the following lists and use data binding to specify the list of items to be displayed.

Attribute:

DataSource: The Bound data source.

DataTextField: displays data text fields.

DataValueField: displays the data value field.

Selectedvalue: value of the selected item.

Event:

SelectedIndexChanged: event when the selected index changes

Introduction to The listbox control

The listbox control is used to create a single-or multiple-choice list control. It is also used to display list data.


The specific code implementation is as follows:

WebForm1.aspx: Page Control

   
WebForm1.aspx. cs: server code

Protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {// display the province SqlConnection con = db in the database in the dropdownlist. createdb (); con. open (); SqlCommand cmd = new SqlCommand ("select * from T_pro", con); SqlDataReader sdr = cmd. executeReader (); this. ddpro. dataSource = sdr; // bind the data source this. ddpro. dataTextField = "proName"; // display the data text this. ddpro. dataValueField = "proID"; // display the data value field this. ddpro. dataBind (); sdr. close (); con. close () ;}} protected void ddpro_SelectedIndexChanged (object sender, EventArgs e) {lcity. items. clear (); SqlConnection con = db. createdb (); con. open (); SqlCommand cmdemp = new SqlCommand ("select * from emp where depID =" + this. ddpro. selectedValue, con); SqlDataReader sdrcity = cmdemp. executeReader (); while (sdrcity. read () {this. lcity. items. add (new ListItem (sdrcity. getString (1), sdrcity. getInt32 (0 ). toString (); // Add the city name of the corresponding province to listbox} sdrcity. close (); con. close ();}}

Db type: connect to the database

    public class db    {        public db()        { }        public static SqlConnection createdb()        {            SqlConnection con = new SqlConnection("server=yiqing-pc;database=pro;uid=sa;pwd=....;");            return con;        }    }
Summary

I remember that two levels of association were used in the test system last year, and I was still thinking about how to do it. Now it is a simple example of two levels of association, however, this simple example shows the principle of two levels of association. In the future, we will have more experiences and gains in this area.


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