Asp. NET implementation of cascading Drop-down Box effect examples _ practical skills

Source: Internet
Author: User

With ASP.net control to achieve departmental and employee linkage, the reference process is as follows
Effect Chart:

Default.aspx code:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%> <! 
 
DOCTYPE html> 
 
 
 

Default.aspx.cs Code:

 using System; 
Using System.Collections.Generic; 
Using System.Linq; 
Using System.Web; 
Using System.Web.UI; 
Using System.Web.UI.WebControls; 
 
Using System.Data.SqlClient; Public partial class _default:system.web.ui.page {protected void Page_Load (object sender, EventArgs e) {if (!th Is. 
  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 (); Displays 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 (); Close connection 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 (); Close connection con. 
 Close (); } 
} 

DBCon.cs Code

Using System; 
Using System.Collections.Generic; 
Using System.Linq; 
Using System.Web; 
Using System.Data.SqlClient; 
 
<summary> 
///Dbcon Summary description 
///</summary> public 
class Dbcon 
{public 
 Dbcon () 
 {//// 
 TODO: Add constructor logic 
 // 
 } public 
 static SqlConnection createconnection () 
 { 
 SqlConnection con = new SqlConnection ("server=.; database=department;uid=sa;pwd=123456 "); 
 Return con. 
 } 
} 

Using the ASP.net control is simpler, but it is best not to use it in the case of a large number of users, and constant requests to the server can put a heavy burden on the server. Using jquery and AJAX implementations can have dynamic effects, the implementation process is more complex, but there is data buffering and Ajax local refresh can reduce the burden on the server, jquery implementation cascading dropdown box effect, reference this article: http://www.jb51.net/article/72366.htm

If you want to learn more, you can click the jquery dropdown box effect summary, JavaScript drop-down Box effect summary to learn.

The above is asp.net implementation of cascading Drop-down Box effect example, I hope we can apply.

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.