国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:29:22
字體:
供稿:網(wǎng)友

這篇文章主要介紹了ASP.NET實(shí)現(xiàn)級(jí)聯(lián)下拉框效果實(shí)例,需要的朋友可以參考下

用ASP.NET控件實(shí)現(xiàn)部門和員工的聯(lián)動(dòng),參考過程如下

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

Default.aspx代碼:

 

 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.  
  3. <!DOCTYPE html>  
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  
  8. <title></title>  
  9. </head>  
  10. <body>  
  11. <form id="form1" runat="server">  
  12. <div>  
  13.  
  14. <asp:DropDownList ID="ddlDep" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlDep_SelectedIndexChanged">  
  15. </asp:DropDownList>  
  16. <br />  
  17. <asp:ListBox ID="lBoxEmp" runat="server"></asp:ListBox>  
  18.  
  19. </div>  
  20. </form>  
  21. </body>  
  22. </html> 

Default.aspx.cs代碼:

 

 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data.SqlClient;  
  8.  
  9. public partial class _Default : System.Web.UI.Page  
  10. {  
  11. protected void Page_Load(object sender, EventArgs e)  
  12. {  
  13. if (!this.IsPostBack)  
  14. {  
  15. SqlConnection con = DBCon.createConnection();  
  16. con.Open();  
  17. //顯示部門  
  18. SqlCommand cmd = new SqlCommand("select * from Tdepartment", con);  
  19. SqlDataReader sdr = cmd.ExecuteReader();  
  20. this.ddlDep.DataSource = sdr;  
  21. this.ddlDep.DataTextField = "depName";  
  22. this.ddlDep.DataValueField = "depID";  
  23. this.ddlDep.DataBind();  
  24. sdr.Close();  
  25. //顯示員工  
  26. SqlCommand cmdEmp =new SqlCommand ("select * from emp where depID=" + this.ddlDep .SelectedValue ,con);  
  27. SqlDataReader sdrEmp = cmdEmp.ExecuteReader();  
  28. while (sdrEmp.Read())  
  29. {  
  30. this.lBoxEmp.Items.Add (new ListItem(sdrEmp.GetString(1),sdrEmp .GetInt32 (0).ToString ()));  
  31. }  
  32. sdrEmp.Close();  
  33. //關(guān)閉連接  
  34. con.Close();  
  35. }  
  36. }  
  37. protected void ddlDep_SelectedIndexChanged(object sender, EventArgs e)  
  38. {  
  39. this.lBoxEmp.Items.Clear();  
  40. SqlConnection con = DBCon.createConnection();  
  41. con.Open();  
  42. SqlCommand cmdEmp = new SqlCommand("select * from emp where depID=" + this.ddlDep.SelectedValue, con);  
  43. SqlDataReader sdrEmp = cmdEmp.ExecuteReader();  
  44. while (sdrEmp.Read())  
  45. {  
  46. this.lBoxEmp.Items.Add(new ListItem(sdrEmp.GetString(1), sdrEmp.GetInt32(0).ToString()));  
  47. }  
  48. sdrEmp.Close();  
  49. //關(guān)閉連接  
  50. con.Close();  
  51. }  
  52. }  

DBCon.cs代碼

 

 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Data.SqlClient;  
  6.  
  7. /// <summary>  
  8. /// DBCon 的摘要說明  
  9. /// </summary>  
  10. public class DBCon  
  11. {  
  12. public DBCon()  
  13. {  
  14. //  
  15. // TODO: 在此處添加構(gòu)造函數(shù)邏輯  
  16. //  
  17. }  
  18. public static SqlConnection createConnection()  
  19. {  
  20. SqlConnection con = new SqlConnection("server=.;database=department;uid=sa;pwd=123456");  
  21. return con;  
  22. }  
  23. }  

使用Asp.net控件實(shí)現(xiàn)比較簡單,但在大量用戶使用的情況下最好不要使用,不斷向服務(wù)器請求會(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),JQuery實(shí)現(xiàn)級(jí)聯(lián)下拉框效果,參考這篇文章:http://m.survivalescaperooms.com/article/72366.htm

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 湘阴县| 金昌市| 文水县| 定兴县| 霍邱县| 新河县| 萨迦县| 柳江县| 南充市| 民权县| 肇庆市| 常山县| 哈密市| 新乡县| 德清县| 新和县| 会泽县| 东丰县| 灵武市| 和林格尔县| 河源市| 涟水县| 怀安县| 锡林浩特市| 贵阳市| 博乐市| 黔西| 康定县| 青川县| 泰安市| 蕉岭县| 乌海市| 贵港市| 五大连池市| 和田市| 巴林左旗| 内黄县| 皮山县| 浪卡子县| 浦北县| 长乐市|