Home Backend Development C#.Net Tutorial asp.net ajax implements no refresh verification code

asp.net ajax implements no refresh verification code

Jan 13, 2017 pm 02:23 PM

1. First, the event code in Page_Load of the aspx file of the background verification code:

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 
} 
} 
}
Copy after login

2. Secondly, define a JS function on the page that displays the verification code

function fGetCode() 
{ 
document.getElementById("getcode").src="Default2.aspx?"+Math.random(); 
}
Copy after login

3. Then edit the front page aspx, the following is the code snippet of the front page

<label>验证码</label> 
<asp:TextBox ID="txt_checkCode" runat="server" Width="178px"></asp:TextBox> 
<img src="Default2.aspx" alt="看不清楚?" id="getcode"/> <a href="javascript:fGetCode()">更换验证码</a>
Copy after login

For more asp.net ajax implementation of non-refresh verification code related articles, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add next-level C compiler How to add next-level C compiler Mar 03, 2025 pm 05:44 PM

How to add next-level C compiler

What are the web versions of C language compilers? What are the web versions of C language compilers? Mar 03, 2025 pm 05:42 PM

What are the web versions of C language compilers?

Method of copying code by C language compiler Method of copying code by C language compiler Mar 03, 2025 pm 05:43 PM

Method of copying code by C language compiler

What are the alternatives to NULL in C language What are the alternatives to NULL in C language Mar 03, 2025 pm 05:37 PM

What are the alternatives to NULL in C language

Which C language compiler is better? Which C language compiler is better? Mar 03, 2025 pm 05:39 PM

Which C language compiler is better?

C language online programming website C language compiler official website summary C language online programming website C language compiler official website summary Mar 03, 2025 pm 05:41 PM

C language online programming website C language compiler official website summary

Is NULL still important in modern programming in C language? Is NULL still important in modern programming in C language? Mar 03, 2025 pm 05:35 PM

Is NULL still important in modern programming in C language?

C language compiler installation tutorial (computer version) C language compiler installation tutorial (computer version) Mar 03, 2025 pm 05:41 PM

C language compiler installation tutorial (computer version)

See all articles