ASP. NET AJAX implementation without refresh verification code

Source: Internet
Author: User
1. First, the event code in the Page_Load of the ASPX file for 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 student Online Exam system {public partial class AjaxAutoCode:System.Web.UI.Page {//Verify digital public string Authcode = string. Empty; protected void Page_Load (object sender, EventArgs e) {#region The first method of generating a verification code random random = new random (); authcode = random . Next (1111, 9999). ToString (); Constructs the picture Bitmap image = New Bitmap (Authcode. Length * 12, 25); Create a 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 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, next in the Display verification code page defines a JS function

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

3. Then edit the foreground page aspx, below is the code snippet for the foreground page

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

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.