. Net does not refresh the new page to refresh the verification code js Implementation Method

Source: Internet
Author: User

In fact, a lot of friends say that ajax is actually not the case. We only need to use js to implement the verification code refresh and replacement function instead of refreshing the page. If you need it, you can refer to it.

The purpose of refreshing the verification code using Javascript is to re-obtain the verification code without refreshing the page.

JS Code:

The Code is as follows: Copy code
Function RefreshCode (obj ){
Obj. src = obj. src + "? Code = "+ Math. random ();
}

 

Call:

The Code is as follows: Copy code

Onclick = "RefreshCode (this)"/>

 


Lower instance (1)

Html page

The Code is as follows: Copy code

<Script>
Function fGetCode ()
{
Document. getElementById ("getcode"). src = "asp.net. aspx? "+ Math. random ();
}
</Script>

<Label> Verification Code </label>
<Asp: TextBox ID = "txt_checkCode" runat = "server" Width = "178px"> </asp: TextBox>
<

Href = "javascript: fGetCode ()"> change the Verification Code </a>

Asp.net. aspx
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 online student Examination System
{
Public partial class AjaxAutoCode: System. Web. UI. Page
{
// Verify the number
Public string authcode = string. Empty;
Protected void Page_Load (object sender, EventArgs e)
{
# Region the first method to generate a verification code
Random random = new Random ();
Authcode = random. Next (1111,999 9). ToString ();
// Construct an image
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 <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 );
// Link two-point lines
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 );
// Foreground Noise
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
}
}
}

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.