20151221:Web复习:添加

<%@ 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</h1>
        <h1>&nbsp;</h1>
        <h1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;添加数据</h1>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>
            &nbsp;</p>
        <p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="Label1" runat="server" Text="代号:"></asp:Label>
            <asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
            &nbsp;</p>
        <p>
            &nbsp;</p>
        <p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="Label2" runat="server" Text="姓名:"></asp:Label>
            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
&nbsp;
        </p>
        <p>
            &nbsp;</p>
        <p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="Label3" runat="server" Text="性别:"></asp:Label>
            <asp:RadioButton ID="rdnan" runat="server" Text="男" Checked="True" GroupName="sex" />
&nbsp;&nbsp;
            <asp:RadioButton ID="rdnv" runat="server" Text="女" GroupName="sex" />
        </p>
        <p>
            &nbsp;</p>
        <p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="Label4" runat="server" Text="民族:"></asp:Label>
            <asp:DropDownList ID="drNation" runat="server">
            </asp:DropDownList>
        </p>
        <p>
            &nbsp;</p>
        <p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Label ID="Label5" runat="server" Text="生日:"></asp:Label>
            <asp:TextBox ID="txtBirthday" runat="server"></asp:TextBox>
            &nbsp;</p>
        <p>
            &nbsp;</p>
        <p>
            &nbsp;</p>
        <p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="确定" />
&nbsp;&nbsp;&nbsp;
            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="返回" />
            &nbsp;</p>
        <p>&nbsp;</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"); } }

posted @ 2015-12-24 22:37  m-n  阅读(184)  评论(0编辑  收藏  举报