ASp.net 注册
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!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> </head> <style type="text/css"> .z { width:600px; margin-left:100px; } .t { margin-left:100px; } </style> <body> <form id="form1" runat="server"> <div style="border:1px solid black;" class="z"> <table border="0" class="t"> <tr> <td><asp:Label ID="Label1" runat="server" Text="姓名:"></asp:Label></td> <td> <asp:TextBox ID="name" runat="server" AutoPostBack="True"></asp:TextBox></td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="姓名不能为空" ControlToValidate="name"></asp:RequiredFieldValidator></td> </tr> <tr> <td><asp:Label ID="Label2" runat="server" Text="密码"></asp:Label> </td> <td><asp:TextBox ID="passwd" runat="server"></asp:TextBox> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="密码不能为空" ControlToValidate="passwd"></asp:RequiredFieldValidator> </td> </tr> <tr> <td><asp:Label ID="Label3" runat="server" Text="密码确认"></asp:Label> </td> <td><asp:TextBox ID="repasswd" runat="server"></asp:TextBox> </td> <td><asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="两次密码不一样" ControlToCompare="passwd" ControlToValidate="repasswd"></asp:CompareValidator> </td> </tr> <tr> <td><asp:Label ID="Label4" runat="server" Text="Emial"></asp:Label> </td> <td> <asp:TextBox ID="email" runat="server"></asp:TextBox> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Emial不能为空" ControlToValidate="email"></asp:RequiredFieldValidator> </td> </tr> <tr> <td><asp:Label ID="Label5" runat="server" Text="性别"></asp:Label> </td> <td> <asp:RadioButton ID="RadioButton1" runat="server" Text="男" AutoPostBack="True" GroupName="rb" OnCheckedChanged="RadioButton1_CheckedChanged" /> <asp:RadioButton ID="RadioButton2" runat="server" Text="女" AutoPostBack="True" GroupName="rb" /> </td> </tr> <tr> <td> <asp:Label ID="Label6" runat="server" Text="选择省份"></asp:Label> </td> <td> <asp:DropDownList ID="addres" runat="server" Height="22px" Width="137px" AutoPostBack="True"></asp:DropDownList> </td> <td><asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="省份不能为空" ControlToValidate="addres"></asp:RequiredFieldValidator> </td> </tr> <tr> <td> <asp:Label ID="Label7" runat="server" Text="爱好"></asp:Label> </td> <td> <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="5" AutoPostBack="True"> <asp:ListItem>体育</asp:ListItem> <asp:ListItem>音乐</asp:ListItem> <asp:ListItem>旅游</asp:ListItem> <asp:ListItem>读书</asp:ListItem> <asp:ListItem>游戏</asp:ListItem> </asp:CheckBoxList> </td> </tr> <tr> <td> <asp:Label ID="Label8" runat="server" Text="备注"></asp:Label> </td> <td> <asp:TextBox ID="remark" runat="server" AutoPostBack="True" Width="255px" Height="76px" TextMode="MultiLine"></asp:TextBox> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="备注为空" ControlToValidate="remark"></asp:RequiredFieldValidator> </td> </tr> <tr> <td> <asp:Label ID="Label9" runat="server" Text="上传照片"></asp:Label> </td> <td> <asp:FileUpload ID="FileUpload1" runat="server" Width="249px" /> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ErrorMessage="文件上传为空" ControlToValidate="FileUpload1"></asp:RequiredFieldValidator> </td> </tr> <tr> <td> <asp:Button ID="Button1" runat="server" Text="注册" OnClick="Button1_Click" /> </td> </tr> </table> <style type="text/css"> .xx { color:red; padding:10px; border-top:1px solid black; } .xx p { margin-left:90px; } </style> <div class="xx" runat="server"> <p><asp:Label ID="Label10" runat="server" Text="您的姓名为:"></asp:Label><asp:Label ID="lname" runat="server" Text="Label"></asp:Label></p> <p><asp:Label ID="Label12" runat="server" Text="您的密码为:"></asp:Label><asp:Label ID="lpasswd" runat="server" Text="Label"></asp:Label></p> <p><asp:Label ID="Label14" runat="server" Text="您的Email为:"></asp:Label><asp:Label ID="lemail" runat="server" Text="Label"></asp:Label></p> <p><asp:Label ID="Label16" runat="server" Text="您的性别为:"></asp:Label><asp:Label ID="lsex" runat="server" Text="Label"></asp:Label></p> <p><asp:Label ID="Label18" runat="server" Text="您的籍贯为:"></asp:Label><asp:Label ID="laddres" runat="server" Text="Label"></asp:Label></p> <p><asp:Label ID="Label20" runat="server" Text="您的爱好是:"></asp:Label><asp:Label ID="llove" runat="server" Text="Label"></asp:Label></p> <p><asp:Label ID="Label22" runat="server" Text="您的备注:"></asp:Label><asp:Label ID="lremark" runat="server" Text="Label"></asp:Label></p> <p><asp:Label ID="Label24" runat="server" Text="您的照片:"></asp:Label></p> <p> <asp:Image ID="Image1" runat="server" Height="136px" Width="130px" /> </p> </div> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Data; using DBHelper; using MySql.Data.MySqlClient; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string sql = "select * from s_province"; DataSet ds = SqlHelper.ExecuteDataSetText(sql, null); this.addres.DataTextField = "ProvinceName"; this.addres.DataValueField = "ProvinceId"; this.addres.DataSource = ds.Tables[0]; this.addres.DataBind(); } } protected void Button1_Click(object sender, EventArgs e) { if (!IsPostBack) { string name = this.name.Text.Trim(); string passwd = this.passwd.Text.Trim(); string email = this.email.Text.Trim(); int sex = 0; if (this.RadioButton1.Checked == true) { sex = 1; } else { sex = 0; } string addres = this.addres.SelectedItem.Text; string love = ""; for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (this.CheckBoxList1.Items[i].Selected == true) { love += this.CheckBoxList1.Items[i].Text.Trim() + ","; } } string remark = this.remark.Text.Trim(); string pos_name = ""; //文件上传 string upload = FileUpload1.PostedFile.FileName; string images = "images/"; string pic_abs = Server.MapPath(images); string pirefix = Path.GetExtension(upload); string daname = ""; if (pirefix != ".jpg") { RequiredFieldValidator9.ErrorMessage = "上传文件格式不正确"; } else { if (!Directory.Exists(pic_abs)) { Directory.CreateDirectory(pic_abs); } //格式 daname = DateTime.Now.Ticks.ToString() + pirefix; //文件名防重复 pos_name = pic_abs + daname; FileUpload1.SaveAs(pic_abs + daname); RequiredFieldValidator9.ErrorMessage = "上传文件成功"; } //添加数据库 string sql = "insert into memberinfo(name,passwd,email,sex,love,remark,imgpath)values(@name,@passwd,@email,@sex,@love,@remark,@imgpath)"; MySqlParameter d1 = new MySqlParameter("@name", name); MySqlParameter d2 = new MySqlParameter("@passwd", passwd); MySqlParameter d3 = new MySqlParameter("@email", email); MySqlParameter d4 = new MySqlParameter("@sex", sex); MySqlParameter d5 = new MySqlParameter("@love", love.Trim(',')); MySqlParameter d6 = new MySqlParameter("@remark", remark); MySqlParameter d7 = new MySqlParameter("@imgpath", pos_name); MySqlParameter[] pa = new MySqlParameter[] { d1, d2, d3, d4, d5, d6, d7 }; int x = SqlHelper.ExecteNonQueryText(sql, pa); if (x > 0) { Response.Write("<script>alert('创建成功')</script>"); } //div选择 this.lname.Text = name; this.lpasswd.Text = passwd; this.lemail.Text = email; this.lsex.Text = ""; if (RadioButton1.Checked == true) { this.lsex.Text = "男"; } else { this.lsex.Text = "女"; } this.llove.Text = love.Trim(','); this.lremark.Text = remark; this.laddres.Text = addres; this.Image1.ImageUrl = @"./images/" + daname; } } protected void RadioButton1_CheckedChanged(object sender, EventArgs e) { } }