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 <P&G t;11 </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)