01-19asp.net基础--网站登录及验证

第一步:

1)首先使用“CodeSmith”将Examinee类实体化,并生成实体类连接数据库的方法,存在解决方案下的“App_Code”文件夹下。

修改一下连接某个数据库;

private SqlConnection _Conn;
        public DBConnection()
        {
            String connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["conn"].ToString();
            _Conn = new SqlConnection(connectionString);
        }

在config修改配置信息:

 1 <configuration>
 2 
 3     <system.web>
 4       <compilation debug="true" targetFramework="4.5" />
 5       <httpRuntime targetFramework="4.5" />
 6     </system.web>
 7   <connectionStrings>
 8     <add name ="conn" connectionString="server=.;database=kaoshixitong;user=sa;pwd=123"/>
 9   </connectionStrings>
10 </configuration>

第二步:首先设计好界面:

2)编写代码并实现功能。

登录代码
 1  //登录按钮
 2     protected void Button1_Click(object sender, EventArgs e)
 3     {
 4         string user = txt_user.Text.Trim();
 5         if (user == "")
 6         {
 7             Response.Write("<font size=16>不能为空</font>");//在网站制作中输出是Response
 8         }
 9         string mima = txt_mima.Text.Trim();
10         ExamineeData da = new ExamineeDA().Select(user,mima);//调用数据库方法
11         if (da!=null)
12         {
13             Response.Redirect("http://baidu.com");
14         }
15         else
16         {
17             Label1.Visible = true;
18         }
19         if (TextBox1.Text.Trim() == LinkButton1.Text)
20         {
21             Response.Write("http://baidu.com");//验证码输入正确,直接跳转到百度
22         }
23         else
24         {
25             Response.Write("验证有误");//输入错误时
26         }
27     }
28     //随机4位数字验证码
29     protected void LinkButton1_Click(object sender, EventArgs e)
30     {
31         Random suiji = new Random();//随机数
32         for (int i = 0; i < 4; i++)
33         {
34             if (i == 0)
35             {
36                 LinkButton1.Text = suiji.Next(0, 10).ToString();
37             }
38             else
39             {
40                 LinkButton1.Text += suiji.Next(0,10).ToString();
41             }
42         }
43     }

3)效果图:

原文地址:https://www.cnblogs.com/xiaoqingshe/p/4234936.html