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需要提交给服务器,否则每次点击选项网页会重新加载,导致选项无法更改)
(二)添加员工
实现的效果:
输入要添加的人员信息,点击添加按钮,将信息添加到数据库中的Info表中。
1 public partial class _Default : System.Web.UI.Page 2 { 3 MyDBDataContext context = new MyDBDataContext(); 4 protected void Page_Load(object sender, EventArgs e) 5 { 6 if (!IsPostBack) 7 { 8 //显示所有民族 9 var q = context.Nation; 10 DropDownList1.DataSource = q; 11 DropDownList1.DataTextField = "Name"; 12 DropDownList1.DataValueField = "Code"; 13 DropDownList1.DataBind(); 14 } 15 } 16 protected void Button1_Click(object sender, EventArgs e) 17 { 18 //获取元素 19 string code = TextBox1.Text; 20 string name = TextBox2.Text; 21 bool sex = Convert.ToBoolean(RadioButtonList1.SelectedValue); 22 string nation = DropDownList1.SelectedValue; 23 DateTime birthday = Convert.ToDateTime(TextBox3.Text); 24 //添加 25 Info data = new Info(); 26 data.Code = code; 27 data.Name = name; 28 data.Sex = sex; 29 data.Nation = nation; 30 data.Birthday = birthday; 31 32 context.Info.InsertOnSubmit(data); 33 context.SubmitChanges(); 34 35 ClearData(); 36 } 37 //清空 38 private void ClearData() 39 { 40 TextBox1.Text = ""; 41 TextBox2.Text = ""; 42 RadioButtonList1.SelectedIndex = 0; 43 DropDownList1.SelectedIndex = 0; 44 TextBox3.Text = ""; 45 } 46 }
(三)修改人员信息
要实现的效果:
点击修改:
点击修改按钮后,将更改的数据更新到数据库中。
1 <body> 2 <form id="form1" runat="server"> 3 <div> 4 <table width="100%" border="0" cellspacing="1" cellpadding="5" bgcolor="#000000"> 5 <tr> 6 <td width="15%" align="center" bgcolor="#336699"><strong>代号</strong></td> 7 <td width="15%" align="center" bgcolor="#336699"><strong>姓名</strong></td> 8 <td width="15%" align="center" bgcolor="#336699"><strong>性别</strong></td> 9 <td width="15%" align="center" bgcolor="#336699"><strong>民族</strong></td> 10 <td width="15%" align="center" bgcolor="#336699"><strong>生日</strong></td> 11 <td width="25%" align="center" bgcolor="#336699"><strong>操作</strong></td> 12 </tr> 13 <% 14 MyDBDataContext context = new MyDBDataContext(); 15 var q = context.Info; 16 foreach(Info data in context.Info) 17 { 18 %> 19 <tr> 20 <td align="center" bgcolor="#33CCFF"><%=data.Code %></td> 21 <td align="center" bgcolor="#33CCFF"><%=data.Name %></td> 22 <td align="center" bgcolor="#33CCFF"><%=data.Sex.Value?"男":"女" %></td> 23 <td align="center" bgcolor="#33CCFF"><%=data.Nation1.Name %></td> 24 <td align="center" bgcolor="#33CCFF"><%=data.Birthday.Value.ToString() %></td> 25 <td align="center" bgcolor="#33CCFF"><a href="edit.aspx?id=<%=data.Code %>">修改</a></td> 26 </tr> 27 <% 28 } 29 %> 30 </table> 31 </div> 32 </form> 33 </body>
1 public partial class edit : System.Web.UI.Page 2 { 3 MyDBDataContext context = new MyDBDataContext(); 4 protected void Page_Load(object sender, EventArgs e) 5 { 6 if (!IsPostBack) 7 { 8 //显示所有民族 9 var q = context.Nation; 10 DropDownList1.DataSource = q; 11 DropDownList1.DataTextField = "Name"; 12 DropDownList1.DataValueField = "Code"; 13 DropDownList1.DataBind(); 14 ShowData(); 15 } 16 } 17 protected void Button1_Click(object sender, EventArgs e) 18 { 19 //获取元素 20 string code = TextBox1.Text; 21 string name = TextBox2.Text; 22 bool sex = Convert.ToBoolean(RadioButtonList1.SelectedValue); 23 string nation = DropDownList1.SelectedValue; 24 DateTime birthday = Convert.ToDateTime(TextBox3.Text); 25 //添加 26 var q = context.Info.Where(p => p.Code == code); 27 Info data = q.First(); 28 data.Code = code; 29 data.Name = name; 30 data.Sex = sex; 31 data.Nation = nation; 32 data.Birthday = birthday; 33 34 context.SubmitChanges(); 35 Response.Redirect("Default2.aspx"); 36 } 37 38 private void ShowData() 39 { 40 if (Request["id"] != null) 41 { 42 string s = Request["id"].ToString(); 43 var q = context.Info.Where(p=>p.Code==s); 44 if (q.Count() > 0) 45 { 46 Info data = q.First(); 47 TextBox1.Text = data.Code; 48 TextBox2.Text = data.Name; 49 RadioButtonList1.SelectedValue = data.Sex.Value.ToString(); 50 DropDownList1.SelectedValue = data.Nation; 51 TextBox3.Text = data.Birthday.ToString(); 52 } 53 } 54 else 55 { 56 Response.Write("没有找到相关信息!<a href='Default2.aspx'>返回</a>"); 57 Response.End(); 58 } 59 } 60 }