周末闲来无事...制作一个登陆和注册的页面,代码页面如下:
登陆页面:
protected void Button1_Click(object sender, EventArgs e)//登录按钮单击事件
{
string str = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;//连接数据库
using (SqlConnection sqlcnn=new SqlConnection(str))//创建Connection对象
{
using (SqlCommand sqlcmm = sqlcnn.CreateCommand())//创建Command对象
{
sqlcmm.CommandText = "select name,password from users";//要执行的数据库语句
sqlcnn.Open();//打开数据库
sqlcmm.ExecuteNonQuery();//执行数据库语句
}
}
Response.Redirect("shoppingcad.aspx");//跳转登陆成功以后的页面
}
protected void Button3_Click(object sender, EventArgs e)//注册按钮单击事件
{
Response.Redirect("reg.aspx");//跳转到注册页面
}
注册页面:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)//注册按钮单击事件
{
if (TextBox6.Text.ToUpper().ToString() == Session["Verification_code"].ToString())//判断验证码(TextBox6中所输内容为验证码)
{
Myencrypt myen = new Myencrypt();
object objs = SqlHelper.ExecuteScalar("insert into users (name,password,email,money,Points) values (@n,@p,@e,'10000','0');select @@IDENTITY",
new SqlParameter("@n", TextBox1.Text),
new SqlParameter("@p", myen.Encryptions(TextBox2.Text)),
new SqlParameter("@e", TextBox4.Text));//写入数据库中所填写的用户名、密码、邮箱
if (objs != null)//判断OBJS
{
FSMail(Convert.ToInt32(objs));//执行发送邮件方法
Response.Redirect("login.aspx");跳转注册成功以后的页面
}
}
else
{
TextBox6.Text = "验证码错误";
TextBox6.Focus();
}
}
private void FSMail(int num)//发送邮件的方法
{
try
{
string strSmtpServer = "smtp.163.com";
string strFrom = "<A href="mailto:zhe72521@163.com";//">zhe72521@163.com";//发件人邮箱
string strFromPass = "******";//发件人密码
string strto = this.TextBox4.Text;//收件人邮箱
string strSubject = "您注册了京东商城账号";//所发邮件标题
string strBody = "恭喜你注册成功,您的用户ID:" + num + ",账号:" + this.TextBox1.Text + ",密码:" + this.TextBox2.Text + ",请用用户ID登录。";//所发邮件内容
System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;
client.Send(message);
}
catch(Exception)
{
}
}
以上为两个页面的后台代码!主要是熟练练习连接数据库的代码,还有要执行的数据库语句!下边是拓展一点小知识:发送邮件!
感觉收获很大,你也试试吧!其实很简单......