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:

 

?
1234567891011121314151617181920212223242526 create database department use department create 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,'Dan',1)insert into emp values(3,'Little girl',1)insert into emp values(4,'Madamame',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.

 

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 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 the 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 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 from initializing the page                 // Close the connection                con.Close();             }        }         protected void ddlDep_SelectedIndexChanged(object sender, EventArgs e)        {            this.lBoxEmp.Items.Clear();             SqlConnection con = DBcon.createConnection();            con.Open();           // Display 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 from initializing the page             // Close 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.

 

?
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;                     }    }}

 

 

 

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.

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.