Today we are going to use ASP. NET to achieve a cascade, this little one should be used frequently.
Let's draw a simple two form. The text box displays different content depending on what you select in the drop-down box.
The specific implementation of the results are as follows
Step One:
Prepare the work and set up the corresponding database.
The display effect is as follows
The attached script is as follows
Create DATABASE Departmentuse departmentcreate table tdepartment (depid int primary KEY, Depname varchar () NOT NULL) Inse RT into tdepartment values (1, ' academic ') insert into tdepartment values (2, ' College ') insert into tdepartment values (3, ' Office ') create Table EMP (empID int primary key,empname varchar () not null,depid int foreign key references tdepartment (depid)) Inse RT into EMP values (1, ' Pony ', 1) insert INTO EMP values (2, ' Little Dan ', 1) insert into EMP values (3, ' Little Sister ', 1) insert INTO EMP values (4, ' Mactan sister ') , 3)
Step Two:
Create a new project for the Asp.ne Web forms application. Two controls in a drawing in a form. DropDownList and a ListBox, named DDLDEP and Lboxemp, respectively. Also set the AutoPostBack property in DropDownList to true (to allow each re-election list to respond).
We need to write the corresponding code in Formload and ddldep_selectedindexchanged time. Where Ddldep_selectedindexchanged is the selected space DDLDEP in the right attribute-event double-click Time SelectedIndexChanged
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 {p rotected 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 (); Show 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 (); Show employees 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 (); Prevent user code here to initialize page//close connection con. Close (); }} protected void Ddldep_selectedindexchanged (object sender, EventArgs e) {this.lboxemp. Items.clear (); SqlConnection con = dbcon.createconnection (); Con. Open (); Show 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 (); Prevent user code here to initialize page//close connection con. Close (); } }}
The above is the main content, the following to get the database statements 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;}}}
Summarize:
We are constantly learning the use of each control, on the one hand we can more flexible use of its methods and features, on the other hand, let us more familiar with each language. Although it is a small step to go, often successful one still feel joy.
ASP. NET control implements data cascade