1、首先是在后台验证码的aspx文件的Page_Load中的事件代码:
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace 学生在线考试系统
{
public partial class AjaxAutoCode : System.Web.UI.Page
{
//验证数字
public string authcode = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
#region 第一种产生验证码的方法
Random random = new Random();
authcode = random.Next(1111, 9999).ToString();
//构造图片
Bitmap image = new Bitmap(authcode.Length * 12, 25);
//创建画布
Graphics g = Graphics.FromImage(image);
try
{
g.Clear(Color.White);
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
//链接两点的线条
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, FontStyle.Bold | FontStyle.Italic);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkBlue, 1.2f, true);
g.DrawString(authcode, font, brush, 2, 2);
//画图片的前景噪点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
ms.WriteTo(this.Response.OutputStream);
ms.Close();
this.Response.ContentType = "image/gif";
}
finally
{
image.Dispose();
g.Dispose();
}
#endregion
}
}
}

2、其次在显示验证码的页面定义一个JS函数
复制代码 代码如下:
function fGetCode()
{
document.getElementById("getcode").src="/UploadFiles/2021-04-02/Default2.aspx?">}

3.再编辑前台页面aspx,下面是前台页面的代码片段
复制代码 代码如下:
<label>验证码</label>
<asp:TextBox ID="txt_checkCode" runat="server" Width="178px"></asp:TextBox>
<img src="/UploadFiles/2021-04-02/Default2.aspx">
标签:
无刷新,验证码

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“asp.net ajax实现无刷新验证码”

暂无“asp.net ajax实现无刷新验证码”评论...

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?