一个需要图文验证的登录程序,请大家帮忙调试下!
我做了一个登录界面,需要输入验证码,但是每次单击登录按钮,就只是刷新我的验证码,到底怎么回事啊,请给位大侠帮忙调试下了!
login.aspx页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="users_login" %>
<!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: 100%;
background-color: #CCFFFF;
}
.style2
{
width: 500px;
}
.style3
{
width: 169px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" cellspacing="1" class="style1">
<tr>
<td>
<table align="center" cellspacing="1" class="style2">
<tr>
<td class="style3" align="right">
用户名:</td>
<td>
<asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3" align="right">
密码:</td>
<td>
<asp:TextBox ID="txt_pwd" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3" align="right">
验证码:</td>
<td>
<asp:TextBox ID="txt_yzm" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" Height="31px" Width ="72px"
ImageUrl="~/users/ValidNums.aspx"/>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
<asp:Button ID="btn_login" runat="server" onclick="btn_login_Click" Text="登录" />
<asp:Button ID="btn_cancel" runat="server" Text="取消" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
login.aspx.cs代码如下
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;
public partial class users_login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_login_Click(object sender, EventArgs e)
{
try
{
if (txt_name.Text == "" || txt_pwd.Text == "")
{
this.Page.RegisterStartupScript("ss", "<script>alter('用户名和密码信息不能为空!')</script>");
return ;//返回操作
}
else
{
string num = this.txt_yzm.Text.Trim();//获取验证码
if (Session["ValidNums"].ToString() == num.ToUpper())
{
//连接数据库
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["ftmydata"].ConnectionString);
sqlcon.Open(); //打开数据库
string strselect = "select * from ft_user where username='" + txt_name.Text.Trim() + "'";
SqlDataAdapter adsas = new SqlDataAdapter(strselect, sqlcon);
DataSet addss = new DataSet();
adsas.Fill(addss); //加载数据信息
if (addss.Tables[0].Rows.Count > 0)
{
string strselect1 = "select * from ft_user where username='" + txt_name.Text.Trim() + "' and userpwd='" + txt_pwd.Text.Trim() + "'";
SqlCommand sqlcom1 = new SqlCommand(strselect1, sqlcon);
SqlDataReader dr1 = sqlcom1.ExecuteReader();
if (dr1.Read()) //执行读取数据信息的操作
{
//登录成功后,跳转到的网站页面
Response.Redirect("default.aspx");
}
//如果密码错误,则弹出提示信息对话框
else
{
this.Page.RegisterStartupScript("ss", "<script>alter('密码错误!')</script>");
return;
}
dr1.Close(); //关闭操作
}
//如果用户名称不存在,则弹出提示对话框
else
{
this.Page.RegisterStartupScript("ss", "<script>alter('用户名不存在!')</script>");
return;
}
sqlcon.Close(); //关闭数据库
}
//如果验证码输入错误,则弹出提示对话框
else
{
this.Page.RegisterStartupScript("ss", "<script>alter('验证码输入错误!')</script>");
return;
}
}
}
//如果有操作上的错误,执行CATCH语句中的程序代码
catch (Exception ex)
{
this.Page.RegisterStartupScript("ss", "<script>alter('验证码输入错误,请刷新页面!')</script>");
}
}
}