前台代码:
<head runat="server">
<title>用户信息添加界面</title><style type="text/css">
.auto-style1 {
height: 23px;
}
.auto-style2 {
height: 25px;
}
p{
color:red;
text-align:center;
}
</style>
</head>
<body style="background-color: #FFE4CA;">
<form id="form1" runat="server">
<div>
<img src="img/zhuce.png" width="100%" height="10%" /></div>
<hr />
<div>
<table border="1" align="center">
<tr>
<td class="auto-style1"><asp:Label ID="Label1" runat="server" Text="姓名"></asp:Label>:
</td>
<td class="auto-style1"><asp:TextBox ID="TextBox1" runat="server" CssClass="style1"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" Display="Dynamic"
ErrorMessage="不能为空"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2"><asp:Label ID="Label2" runat="server" Text="密码"></asp:Label>:
</td>
<td class="auto-style2"><asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="Label3" runat="server" Text="确认密码"></asp:Label>:
</td>
<td><asp:TextBox ID="TextBox3" runat="server" CssClass="style1"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="TextBox3" ControlToCompare="TextBox2" ErrorMessage="两次输入不一致"
Operator="Equal" ></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="auto-style2"><asp:Label ID="Label4" runat="server" Text="年龄"></asp:Label>:
</td>
<td class="auto-style2"><asp:TextBox ID="TextBox4" runat="server" CssClass="style1"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="TextBox4" ErrorMessage="输入的数据不在范围内"
MaximumValue="100" MinimumValue="1" Type="Integer" ></asp:RangeValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text="性别"></asp:Label>:
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="男"></asp:ListItem>
<asp:ListItem Value="女"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td><asp:Label ID="Label6" runat="server" Text="手机号码"></asp:Label>:</td>
<td><asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox5" ValidationExpression = "\d{11}"
ErrorMessage="11位数字"></asp:RegularExpressionValidator>
</td>
</tr>
</table>
</div>
<div align="center">
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="完成" />
   <asp:Button ID="Button2" runat="server" οnclick="Button2_Click" Text="取消" />
</div>
<hr />
<div id="footer" style="background-color:#C0C0C0"><p>Copyright@信息-陈善强</p></div>
</form>
</body>
</html>
前台界面如下:
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Register : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string str;
str = ConfigurationManager.ConnectionStrings["DataConnectStr"].ConnectionString;
SqlConnection conn = new SqlConnection(str);
conn.Open();
string sql = "insert into person1 (name,pwd,age,sex,ph)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox4.Text + "','" + DropDownList1.Text + "','" + TextBox5.Text + "')";
SqlCommand cmd = new SqlCommand(sql, conn);
int i = 0;
using (cmd)
{
i = cmd.ExecuteNonQuery();
}
Response.Write("<script>top.location='GridView1.aspx';</script>");
}