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

(一)三级联动

要实现的效果:

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

代码:

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

     MyDBDataContext context = new MyDBDataContext();
protected void Page_Load(object sender, EventArgs e)
{
//IsPostBack当前请求是否第一次打开,只有在第一次打开的时候是false,其它时候都是true
//在每个Load页面写代码之前,都加上这句话
if (!IsPostBack)
{
ShowProd();
ShowBrand();
ShowCar();
}
}
/// <summary>
/// 显示汽车名称
/// </summary>
private void ShowCar()
{
var q = context.Car.Where(p=>p.Brand==DropDownList2.SelectedValue);
//绑定显示
DropDownList3.DataSource = q;
DropDownList3.DataTextField = "Name";
DropDownList3.DataValueField = "Code";
DropDownList3.DataBind();
}
/// <summary>
/// 显示系列名称
/// </summary>
private void ShowBrand()
{
var q = context.Brand.Where(p=>p.Prod_Code==DropDownList1.SelectedValue);
DropDownList2.DataSource = q;
DropDownList2.DataTextField = "Brand_Name";
DropDownList2.DataValueField = "Brand_Code";
DropDownList2.DataBind();
}
/// <summary>
/// 显示厂商名称
/// </summary>
/// <param name="context"></param>
private void ShowProd()
{
var q = context.Productor;
DropDownList1.DataSource = q;
DropDownList1.DataTextField = "Prod_Name";
DropDownList1.DataValueField = "Prod_Code";
DropDownList1.DataBind();
}
/// <summary>
/// 下拉列表厂商改变选择时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ShowBrand();
ShowCar();
}
/// <summary>
/// 下拉列表系列改变选择时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
ShowCar();
}

三级联动源代码

然后将AutoPostBack属性打为True:  

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

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

(二)添加员工

实现的效果:

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

输入要添加的人员信息,点击添加按钮,将信息添加到数据库中的Info表中。

 public partial class _Default : System.Web.UI.Page
{
MyDBDataContext context = new MyDBDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//显示所有民族
var q = context.Nation;
DropDownList1.DataSource = q;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Code";
DropDownList1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//获取元素
string code = TextBox1.Text;
string name = TextBox2.Text;
bool sex = Convert.ToBoolean(RadioButtonList1.SelectedValue);
string nation = DropDownList1.SelectedValue;
DateTime birthday = Convert.ToDateTime(TextBox3.Text);
//添加
Info data = new Info();
data.Code = code;
data.Name = name;
data.Sex = sex;
data.Nation = nation;
data.Birthday = birthday; context.Info.InsertOnSubmit(data);
context.SubmitChanges(); ClearData();
}
//清空
private void ClearData()
{
TextBox1.Text = "";
TextBox2.Text = "";
RadioButtonList1.SelectedIndex = ;
DropDownList1.SelectedIndex = ;
TextBox3.Text = "";
}
}

添加人员源代码

(三)修改人员信息

要实现的效果:

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

点击修改:

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

点击修改按钮后,将更改的数据更新到数据库中。

 <body>
<form id="form1" runat="server">
<div>
<table width="100%" border="0" cellspacing="1" cellpadding="5" bgcolor="#000000">
<tr>
<td width="15%" align="center" bgcolor="#336699"><strong>代号</strong></td>
<td width="15%" align="center" bgcolor="#336699"><strong>姓名</strong></td>
<td width="15%" align="center" bgcolor="#336699"><strong>性别</strong></td>
<td width="15%" align="center" bgcolor="#336699"><strong>民族</strong></td>
<td width="15%" align="center" bgcolor="#336699"><strong>生日</strong></td>
<td width="25%" align="center" bgcolor="#336699"><strong>操作</strong></td>
</tr>
<%
MyDBDataContext context = new MyDBDataContext();
var q = context.Info;
foreach(Info data in context.Info)
{
%>
<tr>
<td align="center" bgcolor="#33CCFF"><%=data.Code %></td>
<td align="center" bgcolor="#33CCFF"><%=data.Name %></td>
<td align="center" bgcolor="#33CCFF"><%=data.Sex.Value?"男":"女" %></td>
<td align="center" bgcolor="#33CCFF"><%=data.Nation1.Name %></td>
<td align="center" bgcolor="#33CCFF"><%=data.Birthday.Value.ToString() %></td>
<td align="center" bgcolor="#33CCFF"><a href="edit.aspx?id=<%=data.Code %>">修改</a></td>
</tr>
<%
}
%>
</table>
</div>
</form>
</body>

修改员工信息源代码

 public partial class edit : System.Web.UI.Page
{
MyDBDataContext context = new MyDBDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//显示所有民族
var q = context.Nation;
DropDownList1.DataSource = q;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Code";
DropDownList1.DataBind();
ShowData();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//获取元素
string code = TextBox1.Text;
string name = TextBox2.Text;
bool sex = Convert.ToBoolean(RadioButtonList1.SelectedValue);
string nation = DropDownList1.SelectedValue;
DateTime birthday = Convert.ToDateTime(TextBox3.Text);
//添加
var q = context.Info.Where(p => p.Code == code);
Info data = q.First();
data.Code = code;
data.Name = name;
data.Sex = sex;
data.Nation = nation;
data.Birthday = birthday; context.SubmitChanges();
Response.Redirect("Default2.aspx");
} private void ShowData()
{
if (Request["id"] != null)
{
string s = Request["id"].ToString();
var q = context.Info.Where(p=>p.Code==s);
if (q.Count() > )
{
Info data = q.First();
TextBox1.Text = data.Code;
TextBox2.Text = data.Name;
RadioButtonList1.SelectedValue = data.Sex.Value.ToString();
DropDownList1.SelectedValue = data.Nation;
TextBox3.Text = data.Birthday.ToString();
}
}
else
{
Response.Write("没有找到相关信息!<a href='Default2.aspx'>返回</a>");
Response.End();
}
}
}

修改员工编辑页面

上一篇:OI再见


下一篇:Linux学习笔记(1)linux的开关机及重启