這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)怎么在ASP.NET項(xiàng)目中實(shí)現(xiàn)一個(gè)級(jí)聯(lián)下拉框效果,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
用ASP.NET控件實(shí)現(xiàn)部門和員工的聯(lián)動(dòng),參考過程如下
效果圖:
Default.aspx代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="ddlDep" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlDep_SelectedIndexChanged"> </asp:DropDownList> <br /> <asp:ListBox ID="lBoxEmp" runat="server"></asp:ListBox> </div> </form> </body> </html>
Default.aspx.cs代碼:
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 (!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(); //關(guān)閉連接 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(); //關(guān)閉連接 con.Close(); } }
DBCon.cs代碼
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; /// <summary> /// DBCon 的摘要說明 /// </summary> public class DBCon { public DBCon() { // // TODO: 在此處添加構(gòu)造函數(shù)邏輯 // } public static SqlConnection createConnection() { SqlConnection con = new SqlConnection("server=.;database=department;uid=sa;pwd=123456"); return con; } }
使用Asp.net控件實(shí)現(xiàn)比較簡單,但在大量用戶使用的情況下好不要使用,不斷向服務(wù)器請(qǐng)求會(huì)給服務(wù)器帶來很大的負(fù)擔(dān)。使用JQuery和ajax實(shí)現(xiàn)可以有動(dòng)態(tài)效果,實(shí)現(xiàn)過程比較復(fù)雜,但有數(shù)據(jù)緩沖和ajax局部刷新可以減少服務(wù)器的負(fù)擔(dān)
上述就是小編為大家分享的怎么在ASP.NET項(xiàng)目中實(shí)現(xiàn)一個(gè)級(jí)聯(lián)下拉框效果了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文標(biāo)題:怎么在ASP.NET項(xiàng)目中實(shí)現(xiàn)一個(gè)級(jí)聯(lián)下拉框效果-創(chuàng)新互聯(lián)
文章鏈接:http://jinyejixie.com/article38/dseppp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、微信公眾號(hào)、企業(yè)網(wǎng)站制作、網(wǎng)站導(dǎo)航、動(dòng)態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容