添加页面前台HTML代码:
<%@ 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="500px"> <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:TextBox ID="txtPrice" runat="server"></asp:TextBox> </p> <p> </p> <p> <asp:Label ID="Label4" runat="server" Text="水果产地:"></asp:Label> <asp:TextBox ID="txtSource" runat="server"></asp:TextBox> </p> <p> </p> <p> <asp:Label ID="Label5" runat="server" Text="水果库存:"></asp:Label> <asp:TextBox ID="txtNumbers" 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> </div> </form> </body> </html>
添加页面后台C#代码:
public partial class Insert : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { TestDataContext context = new TestDataContext(); Fruit data = new Fruit(); data.Ids = txtCode.Text; data.Name = txtName.Text; data.Price = Convert.ToDecimal(txtPrice.Text); data.Source = txtSource.Text; data.Numbers = Convert.ToInt32(txtNumbers.Text); context.Fruit.InsertOnSubmit(data);//添加 context.SubmitChanges();//提交 Clear(); } protected void Button2_Click(object sender, EventArgs e) { Response.Redirect("1217.aspx");//跳转页面 } public void Clear() { //清空 txtCode.Text = ""; txtName.Text = ""; txtPrice.Text = ""; txtSource.Text = ""; txtNumbers.Text = ""; } }