<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Insert.aspx.cs" Inherits="Insert" %>
<!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>
<style type="text/css">
* {
margin: 0px auto;
padding: 0px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="aa" style="width:400px">
<h1> </h1>
<h1> </h1>
<h1> 添加数据</h1>
<p> </p>
<p> </p>
<p>
</p>
<p>
<asp:Label ID="Label1" runat="server" Text="代号:"></asp:Label>
<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
</p>
<p>
</p>
<p>
<asp:Label ID="Label2" runat="server" Text="姓名:"></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</p>
<p>
</p>
<p>
<asp:Label ID="Label3" runat="server" Text="性别:"></asp:Label>
<asp:RadioButton ID="rdnan" runat="server" Text="男" Checked="True" GroupName="sex" />
<asp:RadioButton ID="rdnv" runat="server" Text="女" GroupName="sex" />
</p>
<p>
</p>
<p>
<asp:Label ID="Label4" runat="server" Text="民族:"></asp:Label>
<asp:DropDownList ID="drNation" runat="server">
</asp:DropDownList>
</p>
<p>
</p>
<p>
<asp:Label ID="Label5" runat="server" Text="生日:"></asp:Label>
<asp:TextBox ID="txtBirthday" runat="server"></asp:TextBox>
</p>
<p>
</p>
<p>
</p>
<p>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="确定" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="返回" />
</p>
<p> </p>
</div>
</form>
</body>
</html>
后台C#代码:
public partial class Insert : System.Web.UI.Page
{
private TextDataContext context = new TextDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if(Session["uid"] != null)
{
if (!IsPostBack)
{
//给民族下拉列表绑定数据
drNation.DataSource = context.Nation;
drNation.DataTextField = "Name";
drNation.DataValueField = "Code";
drNation.DataBind();
}
}
else
{
Response.Redirect("Denglu.aspx");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Info data = new Info();//造对象
//赋值
data.Code = txtCode.Text;
data.Name = txtName.Text;
data.Sex = rdnan.Checked;
data.Nation = drNation.SelectedValue;
data.Birthday = Convert.ToDateTime(txtBirthday.Text);
//添加到数据库
context.Info.InsertOnSubmit(data);
context.SubmitChanges();//提交
Clear();
}
public void Clear()
{
//清空
txtCode.Text = "";
txtName.Text = "";
rdnan.Checked= true;
drNation.SelectedIndex = 0;
txtBirthday.Text = "";
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("Main.aspx");
}
}