ajax+ Image Verification Code (general processing program)

Source: Internet
Author: User

Create an empty Web site, drag a TextBox tool into the Design interface Toolbox, a button, plus an image Image tool (as a number, as a letter in a picture). However, the verification code will appear a problem, each time click on the button, the interface automatically submitted again, re-refresh again and return, in order to prevent the entire page is resubmitted, need to add a UpdatePanel, only refresh the current UpdatePanel content.

Must be combined with Ajax to use

After the interface design, you need to add a ashx end of the file entry, here to write the bitmap random verification code format and so on.

 1 <%@ webhandler language= "C #" class= "Code"%> 2 3 using System; 4 using System.Web; 5 using System.Drawing; 6 using System.Drawing.Drawing2D; 7 using System.Drawing.Imaging; 8 using System.Web.SessionState; 9//General handler to use the session, you must inherit the IRequiresSessionState interface (the interface is an empty method), the session exists in the interface of the public class Code:ihttphandler, IRequiresSessionState {One-to-one public void ProcessRequest (HttpContext context) {the context. Response.ContentType = "Image/jpeg"; Bitmap img = new Bitmap (50, 20);//bitmap, draw a blank graphic graphics g = Graph Ics.         FromImage (IMG);//16-string s = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 18 String str = "", Random rand = new Random ();//Initialize random number for (int i = 0; I < 4; i++) {int start = rand. Next (62); Generates a random starting position of up to 1 str + = s.substring (start). ToString ();}25 context. session["Code" = str;//session is used to transmit the value of Font font = new font ("Arial", "FontStyle.Bold");//Set font format SolidBrush brush = new SolidBrush (color.white); 29 G.fillrectangle (brush, 0, 0, +), brush. Color = color.red;31 g.drawstring (str, font, brush, 0, 0); Save (context. Response.outputstream, imageformat.jpeg);}34 public bool IsReusable {- false;38}39}40 41}
General handlers: There is a page a, passing parameters to the general handler, the handler receives parameters, accesses the database, judges correctly, jumps to the next page, errors, jumps to another page.

In the JS source code of ASPX, write function statements to ensure that parameters are passed

 1 <body> 2 <form id= "Form1" runat= "Server" > 3 <div> 4 5 6 <asp:scriptmanag Er id= "ScriptManager1" runat= "Server" > 7 </asp:ScriptManager> 8 9 </div>10 &LT;P&G  t;11 &nbsp;</p>12 <asp:image id= "Image1" runat= "Server" imageurl= "~/code.ashx" />14 <asp:textbox id= "TextBox1" runat= "Server" ></asp:textbox>16 <asp : Button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "button"/>17 <asp:label id= "Label1 "runat=" Server "text=" failed "></asp:label>18 </form>19 </body>20 

ajax+ Image Verification Code (general processing program)

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.