ASP.net controls implement data cascade and asp.net controls

Source: Internet
Author: User

ASP.net controls implement data cascade and asp.net controls

Today, we are going to use ASP.net to implement a cascade. This small point should be frequently used.


Let's simply draw two forms. The text box displays different content based on the selected content in the drop-down box.

The specific implementation result is as follows:



Step 1:

Prepare and create a database.

The display effect is as follows:


The script is as follows:

Create database departmentuse departmentcreate table TDepartment (depID int primary key, depName varchar (30) not null) insert into TDepartment values (1, 'authorization') insert into TDepartment values (2, 'University ') insert into TDepartment values (3, 'Office') create table emp (empID int primary key, empName varchar (30) not null, depID int foreign key references TDepartment (depID) insert into emp values (1, 'pony ', 1) insert into emp values (2, 'Little dan', 1) insert into emp values (3, 'xiaomei ', 1) insert into emp values (4, 'madanmei', 3)



Step 2:

Create an ASP. ne Web form application. Draw two widgets in the graph in the form. DropDownList and ListBox are named ddlDep and lBoxEmp respectively. At the same time, set the AutoPostBack attribute in the DropDownList to TRUE (to make the reselect list have a response ).
We need to write the corresponding code in formload and ddlDep_SelectedIndexChanged time. DdlDep_SelectedIndexChanged is the selected Space ddlDep. Double-click the time SelectedIndexChanged in properties-events on the right.

Using System; using System. collections. generic; using System. web; using System. web. UI; using System. web. UI. webControls; using System. data. sqlClient; // database namespace department {public partial class WebForm1: System. web. UI. page {protected System. web. UI. webControls. listBox lBoxEmp; protected System. web. UI. webControls. dropDownList ddlDep; protected void Page_Load (object sender, EventArgs e) {if (! This. isPostBack) {SqlConnection con = DBcon. createConnection (); con. open (); // display Department SqlCommand cmd = new SqlCommand ("select * from TDepartment", con); SqlDataReader sdr = cmd. executeReader (); this. ddlDep. dataSource = sdr; this. ddlDep. dataTextField = "depName"; this. ddlDep. dataValueField = "depID"; this. ddlDep. dataBind (); sdr. close (); // display employee SqlCommand cmdEmp = new SqlCommand ("select * from emp where depID =" + this. ddlDep. selectedValue, con); SqlDataReader sdrEmp = cmdEmp. executeReader (); while (sdrEmp. read () {this. lBoxEmp. items. add (new ListItem (sdrEmp. getString (1), sdrEmp. getInt32 (0 ). toString ();} sdrEmp. close (); // prevents user code from initializing the page // closes the connection con. close () ;}} protected void ddlDep_SelectedIndexChanged (object sender, EventArgs e) {this. lBoxEmp. items. clear (); SqlConnection con = DBcon. createConnection (); con. open (); // display employee SqlCommand cmdEmp = new SqlCommand ("select * from emp where depID =" + this. ddlDep. selectedValue, con); SqlDataReader sdrEmp = cmdEmp. executeReader (); while (sdrEmp. read () {this. lBoxEmp. items. add (new ListItem (sdrEmp. getString (1), sdrEmp. getInt32 (0 ). toString ();} sdrEmp. close (); // prevents user code from initializing the page // closes the connection con. close ();}}}

The above is the main content, and the statements for obtaining the database are written below. This is a complete small function.

using System;using System.Data.SqlClient;namespace department{    public class DBcon    {        public DBcon(){ }        public static SqlConnection createConnection()        {            SqlConnection con = new SqlConnection("server=.;database=department;uid=sa;pwd=123456;");            return con;                    }    }}


Summary:

We are constantly learning how to use every control. On the one hand, we can use its methods and features more flexibly, and on the other hand, we can be more familiar with every language. Although it is a small step, every success is still a joy.





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.