ASP. NET control implements data cascade

Source: Internet
Author: User

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

?
1234567891011121314151617181920212223242526 create database departmentuse departmentcreate table TDepartment( depID int primary key, depName varchar(30) not null)insert into TDepartment values(1,‘教务‘)insert into TDepartment values(2,‘高校‘)insert into TDepartment values(3,‘办公室‘)create table emp(    empID int primary  key,    empName varchar(30) not null,    depID int foreign key references TDepartment(depID)    )insert into emp values(1,‘小马‘,1)insert into emp values(2,‘小丹‘,1)insert into emp values(3,‘小妹‘,1)insert into emp values(4,‘马丹妹‘,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

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768 using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient;//数据库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();                //显示部门                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();                //显示员工                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();                //此处防止用户代码以初始化页面                //关闭连接                con.Close();            }        }        protected void ddlDep_SelectedIndexChanged(object sender, EventArgs e)        {            this.lBoxEmp.Items.Clear();            SqlConnection con = DBcon.createConnection();            con.Open();           //显示员工            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();            //此处防止用户代码以初始化页面             //关闭连接            con.Close();        }    }}

The above is the main content, the following to get the database statements are written below. This is a complete small function.

?
123456789101112131415 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

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.