ASP.NET實現(xiàn)級聯(lián)下拉框效果實例講解
用ASP.NET控件實現(xiàn)部門和員工的聯(lián)動,參考過程如下
效果圖:
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(); //關閉連接 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(); } }
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: 在此處添加構造函數(shù)邏輯 // } public static SqlConnection createConnection() { SqlConnection con = new SqlConnection("server=.;database=department;uid=sa;pwd=123456"); return con; } }
使用Asp.net控件實現(xiàn)比較簡單,但在大量用戶使用的情況下最好不要使用,不斷向服務器請求會給服務器帶來很大的負擔。使用JQuery和ajax實現(xiàn)可以有動態(tài)效果,實現(xiàn)過程比較復雜,但有數(shù)據(jù)緩沖和ajax局部刷新可以減少服務器的負擔,JQuery實現(xiàn)級聯(lián)下拉框效果,參考這篇文章://chabaoo.cn/article/72366.htm
如果大家還想深入學習,可以點擊jquery下拉框效果匯總、JavaScript下拉框效果匯總進行學習。
以上就是ASP.NET實現(xiàn)級聯(lián)下拉框效果實例講解,希望大家可以學以致用。
- ASP.NET?MVC實現(xiàn)多選下拉框
- 詳解ASP.NET MVC 下拉框的傳值的兩種方式
- ASP .NET 可編輯輸入自動匹配的下拉框
- 詳解ASP.NET MVC之下拉框綁定四種方式
- ASP.NET MVC下拉框聯(lián)動實例解析
- ASP.NET中DropDownList下拉框列表控件綁定數(shù)據(jù)的4種方法
- ASP.NET多彩下拉框開發(fā)實例
- asp.net mvc下拉框Html.DropDownList 和DropDownListFor的常用方法
- asp.net中js+jquery添加下拉框值和后臺獲取示例
- asp.net 實現(xiàn)下拉框只讀功能
- ASP.NET?MVC下拉框中顯示枚舉項
相關文章
ASP.NET中Session和Cache的區(qū)別總結
這篇文章主要介紹了ASP.NET中Session和Cache的區(qū)別總結,本文結合使用經(jīng)驗,總結出了5點Session緩存和Cache緩存的區(qū)別,需要的朋友可以參考下2015-06-06