佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > ASP.NET實現(xiàn)級聯(lián)下拉框效果實例講解

ASP.NET實現(xiàn)級聯(lián)下拉框效果實例講解

熱門標簽:網(wǎng)站排名優(yōu)化 阿里云 集中運營管理辦法 科大訊飛語音識別系統(tǒng) 百度競價排名 硅谷的囚徒呼叫中心 地方門戶網(wǎng)站 服務(wù)器配置

用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(); 
 //關(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控件實現(xiàn)比較簡單,但在大量用戶使用的情況下最好不要使用,不斷向服務(wù)器請求會給服務(wù)器帶來很大的負擔。使用JQuery和ajax實現(xiàn)可以有動態(tài)效果,實現(xiàn)過程比較復雜,但有數(shù)據(jù)緩沖和ajax局部刷新可以減少服務(wù)器的負擔,JQuery實現(xiàn)級聯(lián)下拉框效果,參考這篇文章://www.jb51.net/article/72366.htm

如果大家還想深入學習,可以點擊jquery下拉框效果匯總、JavaScript下拉框效果匯總進行學習。

以上就是ASP.NET實現(xiàn)級聯(lián)下拉框效果實例講解,希望大家可以學以致用。

您可能感興趣的文章:
  • asp.net省市三級聯(lián)動的DropDownList+Ajax的三種框架(aspnet/Jquery/ExtJs)示例
  • ASP.NET MVC下拉框聯(lián)動實例解析
  • asp.net DropDownList實現(xiàn)二級聯(lián)動效果
  • ASP.NET中DropDownList和ListBox實現(xiàn)兩級聯(lián)動功能
  • asp.net下使用AjaxPro實現(xiàn)二級聯(lián)動代碼
  • asp.net DropDownList 三級聯(lián)動下拉菜單實現(xiàn)代碼
  • asp.net兩級聯(lián)動(包含添加和修改)
  • 適用與firefox ASP.NET無刷新二級聯(lián)動下拉列表
  • ASP.NET Ajax級聯(lián)DropDownList實現(xiàn)代碼
  • jQuery+Asp.Net實現(xiàn)省市二級聯(lián)動功能的方法

標簽:廣西 西雙版納 梧州 烏蘭察布 威海 開封 隨州 甘孜

巨人網(wǎng)絡(luò)通訊聲明:本文標題《ASP.NET實現(xiàn)級聯(lián)下拉框效果實例講解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    永宁县| 武强县| 孟津县| 新乡县| 河间市| 潼南县| 湖口县| 平远县| 汶上县| 宁蒗| 竹溪县| 巴楚县| 扬中市| 新疆| 池州市| 曲周县| 察隅县| 昌宁县| 临潭县| 灯塔市| 师宗县| 潜江市| 白山市| 石林| 四会市| 莲花县| 青神县| 台南市| 灵寿县| 九龙县| 光泽县| 天柱县| 阳西县| 隆子县| 黎城县| 格尔木市| 铜陵市| 潼南县| 徐州市| 广德县| 隆子县|