asp.net Ajax implementation without refreshing verification code _ practical skills

Source: Internet
Author: User
1. The event code is first in the Page_Load of the ASPX file in the background verification code:
Copy Code code as follows:

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 Student Online Examination system
{
public partial class AjaxAutoCode:System.Web.UI.Page
{
Verifying numbers
public string Authcode = string. Empty;
protected void Page_Load (object sender, EventArgs e)
{
#region the first way to generate a validation code
Random Random = new Random ();
Authcode = random. Next (1111, 9999). ToString ();
Construct a picture
Bitmap image = New Bitmap (Authcode. Length * 12, 25);
Create Canvas
Graphics g = graphics.fromimage (image);
Try
{
G.clear (Color.White);
for (int i = 0; i < 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);
Link two-point line
G.drawline (New Pen (color.silver), x1, y1, x2, y2);
}
Font font = new Font ("Arial", 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);
Picture of the foreground noise
for (int i = 0; i < 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, followed by the display of verification code page to define a JS function
Copy Code code as follows:

function Fgetcode ()
{
document.getElementById ("GetCode"). src= "Default2.aspx?" +math.random ();
}

3. Then edit the front page aspx, below is the code snippet for the front page
Copy Code code as follows:

<label> Verification Code </label>
<asp:textbox id= "Txt_checkcode" runat= "Server" width= "178px" ></asp:TextBox>
<a href=" Javascript:fgetcode () "> Replace Verification Code </a>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.