2014.12.06 ASP.NET 三级联动,添加员工,修改员工

(一)三级联动

要实现的效果:

代码:

 1     MyDBDataContext context = new MyDBDataContext();
 2     protected void Page_Load(object sender, EventArgs e)
 3     {
 4         //IsPostBack当前请求是否第一次打开,只有在第一次打开的时候是false,其它时候都是true
 5         //在每个Load页面写代码之前,都加上这句话
 6         if (!IsPostBack)
 7         {
 8             ShowProd();
 9             ShowBrand();
10             ShowCar();
11         }
12     }
13     /// <summary>
14     /// 显示汽车名称
15     /// </summary>
16     private void ShowCar()
17     {
18         var q = context.Car.Where(p=>p.Brand==DropDownList2.SelectedValue);
19         //绑定显示
20         DropDownList3.DataSource = q;
21         DropDownList3.DataTextField = "Name";
22         DropDownList3.DataValueField = "Code";
23         DropDownList3.DataBind();
24     }
25     /// <summary>
26     /// 显示系列名称
27     /// </summary>
28     private void ShowBrand()
29     {
30         var q = context.Brand.Where(p=>p.Prod_Code==DropDownList1.SelectedValue);
31         DropDownList2.DataSource = q;
32         DropDownList2.DataTextField = "Brand_Name";
33         DropDownList2.DataValueField = "Brand_Code";
34         DropDownList2.DataBind();
35     }
36     /// <summary>
37     /// 显示厂商名称
38     /// </summary>
39     /// <param name="context"></param>
40     private void ShowProd()
41     {
42         var q = context.Productor;
43         DropDownList1.DataSource = q;
44         DropDownList1.DataTextField = "Prod_Name";
45         DropDownList1.DataValueField = "Prod_Code";
46         DropDownList1.DataBind();
47     }
48     /// <summary>
49     /// 下拉列表厂商改变选择时
50     /// </summary>
51     /// <param name="sender"></param>
52     /// <param name="e"></param>
53     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
54     {
55         ShowBrand();
56         ShowCar();
57     }
58     /// <summary>
59     /// 下拉列表系列改变选择时
60     /// </summary>
61     /// <param name="sender"></param>
62     /// <param name="e"></param>
63     protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
64     {
65         ShowCar();
66     }
三级联动源代码

然后将AutoPostBack属性打为True:  

(与WinForm不同,B/S需要提交给服务器,否则每次点击选项网页会重新加载,导致选项无法更改)

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。