注册界面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="homework_text.aspx.cs" Inherits="homework_text" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 40%;
}
.style2
{
height: 6px;
}
.style3
{
height: 6px;
text-align: right;
}
.style4
{
text-align: right;
font-weight: bold;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table class="style1">
<tr>
<td class="style4">
用户名:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
密码:</td>
<td>
<asp:TextBox ID="TextBox3" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
确认密码:</td>
<td>
<asp:TextBox ID="TextBox4" runat="server" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="TextBox4" ControlToValidate="TextBox3"
ErrorMessage="两次输入的不一样" style="font-weight: 700; color: #FF3300"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="style4">
E-mail:</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
QQ:</td>
<td>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td class="style2">
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="注册" />
</td>
</tr>
</table>
<br />
<br />
</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.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Net;
public partial class homework_text : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string str_con = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
using (SqlConnection con = new SqlConnection(str_con))
{
using (SqlCommand cmd = new SqlCommand("insert User values(@name,@password,@QQ,@mail,0);select @@identity", con))
{
cmd.Parameters.Add("@name", SqlDbType.VarChar, 20).Value=TextBox1.Text;
cmd.Parameters.Add("@password", SqlDbType.VarChar, 30).Value = TextBox3.Text;
cmd.Parameters.Add("@QQ",TextBox6.Text);
cmd.Parameters.Add("@mail",TextBox5.Text);
con.Open();
int id =Convert.ToInt32(cmd.ExecuteScalar());
MailMessage msg = new MailMessage();
msg.Subject = "尊敬的用户你注册成功!";
msg.From = new MailAddress("l569511751@163.com");
msg.Body = "<a href="+"http://localhost:1797/mail_homework/Default.aspx?id="+id.ToString()+">点击进行帐户的激活</a>";
msg.IsBodyHtml = true;
msg.To.Add(TextBox5.Text);
SmtpClient st = new SmtpClient("smtp.QQ.com");
st.Credentials = new NetworkCredential("l569511751@163.com", "he248191091");
st.Send(msg);
Page.ClientScript.RegisterClientScriptBlock(GetType(), "asd", "<script>alert('注册成功!请您查看邮箱,进行激活')</script>");
}
}
}
}
登陆页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 30%;
}
.style2
{
text-align: right;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td class="style2">
用户名:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
密码:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="登录" />
</td>
</tr>
</table>
</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.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request["id"] != null)
{
string str_con = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
using (SqlConnection con = new SqlConnection(str_con))
{
using (SqlCommand cmd = new SqlCommand("update User set static=1 where id=" + Request["id"].ToString(), con))
{
con.Open();
cmd.ExecuteNonQuery();
Page.ClientScript.RegisterClientScriptBlock(GetType(), "asd", "<script>alert('激活成功!')</script>");
}
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string str_con = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
using (SqlConnection con = new SqlConnection(str_con))
{
using (SqlCommand cmd = new SqlCommand("select * from User where name=@name and password=@pwd", con))
{
con.Open();
cmd.Parameters.Add("@name",TextBox1.Text);
cmd.Parameters.Add("@pwd",TextBox2.Text);
DataTable dt = new DataTable();
using (SqlDataReader myreader = cmd.ExecuteReader())
{
dt.Load(myreader);
}
if (dt.Rows.Count == 1)
{
if (dt.Rows[0][5].ToString() == "1")
{
Page.ClientScript.RegisterClientScriptBlock(GetType(), "asd", "<script>alert('登录成功!')</script>");
}
else
{
Page.ClientScript.RegisterClientScriptBlock(GetType(), "asd", "<script>alert('本帐户还没有激活!')</script>");
}
}
else
{
Page.ClientScript.RegisterClientScriptBlock(GetType(), "asd", "<script>alert('帐户或密码不正确!')</script>");
}
}
}
}
}