首页 > 编程 > .NET > 正文

ASP.NET中的无刷新验证码的开发(完整代码)

2024-07-10 12:42:06
字体:
来源:转载
供稿:网友
代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="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 id="Head1" runat="server">
<title>无标题页</title>
<script type="text/javascript">
function DoFresh() {
document.getElementById("Image1").src = "VerifyCode.aspx";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
验证码:<asp:TextBox ID="txtValidateCode" runat="server"></asp:TextBox>
</td>
<td>
<asp:Image ID="Image1" runat="server" />
<a href="javascript:DoFresh();">看不清?</a>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<br />
<asp:Literal ID="litErrorMsg" runat="server"></asp:Literal>
<asp:Button ID="btnSubmit" runat="server" Text="确定" onclick="btnSubmit_Click" />
</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;

public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Image1.ImageUrl = "VerifyCode.aspx";
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Session["ValidateCode"] != null)
{
string outputValidateCode = Session["ValidateCode"] as string;
string inputValidateCode = txtValidateCode.Text.Trim();
if (string.Compare(outputValidateCode, inputValidateCode, true) != 0)
{
//Response.Write("<script>javascript:alert('输入的验证码错误!');</script>");
litErrorMsg.Text = "输入的验证码错误!";
}
else
{
//Response.Write("<script>javascript:alert('输入的验证码正确!');</script>");
litErrorMsg.Text = "输入的验证码正确!";
}
}
}
#region 调用下面的方法实现客户端保存Cookie验证模式
private void ValidateMethod()
{
if (Request.Cookies["CheckCode"] == null)
{
litErrorMsg.Text = "您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
litErrorMsg.Visible = true;
return;
}
if (String.Compare(Request.Cookies["CheckCode"].Value, TextBox1.Text.ToString().Trim(), true) != 0)
{
litErrorMsg.Text = "<font color=red>对不起,验证码错误!</font>";
litErrorMsg.Visible = true;
return;
}
else
{
litErrorMsg.Text = "<font color=green>恭喜,验证码输入正确!</font>";
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表