我在这次做项目中,担任着注册和登录界面,首先我想大家介绍一下我的代码:
this.Response.Redirect("");//这一句可实现页面的转跳!
this.Response.Write("");//这一句可以实现在网页输出你所想输出的字!
if (this.txtname.Text == "") //如果用户名为空值!则执行花括号里面的!
{
this.Label5.Text = "用?户§名?不?能ü为a空?!?";
}
else if (this.password.Text == "")//如果密码为空值则执行花括号里面的命令!
{
this.Label5.Text="密ü码?不?能ü为a空?!?";
}
else
if (Session["yanzheng"] != null &&
TextBox2.Text != Session["yanzheng"].ToString())
///验证码不能为空或不等于所提示的字母或文字
{
Label4.Text = "验é证¤码?错洙?误ó!?";
return;
}
else
{
Label4.Text = "验é证¤码?正y确ā?!?";
String str = ConfigurationManager.ConnectionStrings["strcnn"].ConnectionString;
//上面这条语句是用来连接数据库(在config文件里面定义名称strcnn)把名称传递过来
using (SqlConnection sqlcnn = new SqlConnection(str))
{
SqlCommand sqlcmm = sqlcnn.CreateCommand();
sqlcmm.CommandText = "insert into siqi(quser,qpass,qlren,qpart)values(@user,@pass,@mail,@name)";///用insert通过C#语句来添加数据库
sqlcmm.Parameters.Add("@user", System.Data.SqlDbType.NVarChar, 50).Value = this.txtname.Text;//把用户名文本框写入的用户名增添到数据库中
sqlcmm.Parameters.Add("@pass", System.Data.SqlDbType.VarChar, 50).Value = this.password.Text;//把用户所输入的密码增加大数据库中
sqlcmm.Parameters.Add("@mail", System.Data.SqlDbType.NChar, 10).Value = this.txtbox.Text;
//添加email
sqlcmm.Parameters.Add("@name", System.Data.SqlDbType.NChar, 10).Value = this.user.Text;
sqlcnn.Open();//打开数据库
sqlcmm.ExecuteReader();//重载
sqlcnn.Close();//关闭数据库
}
}
}
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.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;
Random rd = new Random(); //创建随机数对象
//以下行,产生由个字母和数字组成的一个字符串
string str = "ABCDEFGHIJKLMNOPQRSTURWXYZ0123456789想象是无限的?";
string yanzheng = "";
for (int i = 0; i < 6; i++)
{
yanzheng = yanzheng + str.Substring(rd.Next(42), 1);
}
//验证码值存放到Session中用来比较
Session["yanzheng"] = yanzheng;
//以下三句,通过随机找一个现有图象产生一个画布Bitmap
string bgFilePath = Server.MapPath("images/bg_" + new Random().Next(5) + ".jpg");//随机找个图象
System.Drawing.Image imgObj = System.Drawing.Image.FromFile(bgFilePath);
Bitmap newBitmap = new Bitmap(imgObj, 290, 80);//建立位图对象
Graphics g = Graphics.FromImage(newBitmap);//根据上面创洹建¨的位图对象创洹建¨绘图面
SolidBrush brush = new SolidBrush(Color.Blue);//设置画-笔括颜色
//定¨义一个含10种字体的数组哩
String[] ziti ={ "Arial", "Verdana", "Comic Sans MS", "Impact", "Haettenschweiler", "Lucida Sans Unicode", "Garamond", "Courier New", "Book Antiqua", "Arial Narrow" };
//通过循-环·,绘制每个字符,
for (int a = 0; a < yanzheng.Length; a++)
{
Font textFont = new Font(ziti[rd.Next(9)], 30, FontStyle.Bold);//字体随机,字号大小30,加粗?
//每次循环绘制一个字符,设置字体格式,画笔括颜色,字符相对画布的X坐标括字符相对画布的Y坐标括
g.DrawString(yanzheng.Substring(a, 1), textFont, brush, 40 + a * 36, 20);
}
//保馈存画的图片到输出流中
newBitmap.Save(Response.OutputStream, ImageFormat.Gif);
}
//上面所写的就是用验证码来来注册页面
string sql = "select power from siqi where quser=@name and qpass=@pass";//根据power值来判断用户所输入的权限,来判断使用户登录还是普通登录。
try
{
object obj = SqlHelper.ExecuteScalar(sql,
new SqlParameter("@name", txtUser.Text),
new SqlParameter("@pass", txtPassword.Text));
if (obj == null)
this.Label4.Text="用?户§名?或ò密ü码?错洙?误ó!?";
else
{
int power = Convert.ToInt32(obj);
NewsUser user = new NewsUser(txtUser.Text, power);
Session["user"] = user;
if (power == 1)
{Session["yonghu"]=this.txtUser.Text;
this.Response.Redirect("dengluhou.aspx");
}
else if (power == 2)
this.Response.Redirect("thingsList.aspx");
}
}
catch (Exception ex)
{
this.Response.Write(ex.Message);
this.Response.Write("网?络?故ê障?,?请?联系μ管ü理え?员±!?");
}
//上面是用来登录的页面所需的语句