ASP .NET生成验证码的插件,引用即可使用
ASP .NET生成验证码的插件,引用即可使用<div id="cnblogs_post_body"> ASP .NET验证码dll,引用即可使用。
使用方法:
(1)添加引用。下载tangyouwei.dll打开Visual Studio 2010,新建一个ASP .NET空Web程序,将tangyouwei.dll右键添加引用。
http://www.xinvalue.com/uploads/allimg/121112/1-121112231305303.png
(2)建立验证码页面。新建一个Web窗体,命名VatificationCode.aspx,在页面头部添加using;
http://www.xinvalue.com/uploads/allimg/121112/1-121112232615531.png
在后台VatificationCode.aspx.cs的Page_Load方法中添加如下代码(为了扩展也可以添加ascx用户控件,本文以aspx页面为例):
VerificationCode vc = new VerificationCode();
stringvcode=vc.GetVarificationCode();
Session["vCode"] = vcode;
http://www.xinvalue.com/uploads/allimg/121112/1-121112232U6192.png
(3)调用验证码页面。
新建一个Web窗体,名为Test.aspx,在body的div内写上以下代码:
<img id="Image1" src="VarificationCode.aspx" alt="点击刷新验证码"/>
http://www.xinvalue.com/uploads/allimg/121112/1-12111223201D35.png
在Test.aspx右键->在浏览器中查看,即可看到以下界面。
http://www.xinvalue.com/uploads/allimg/121112/1-121112230954524.png
此时的页面还有缺陷,因为他不能动态更新,要实现动态更新,只需将Test.aspx加上JS即可,完整的代码:
<div class="cnblogs_code"><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="TestProject.Test" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <scripttype="text/javascript"> function RefreshCode() { var randomnum = Math.random(); var img1 = document.getElementById("Image1"); img1.src = "VarificationCode.aspx?" + randomnum; } </script></head><body> <form id="form1" runat="server"> <div> <img id="Image1" src="VarificationCode.aspx" alt="点击刷新验证码"style="cursor: pointer;" onclick="RefreshCode()" /> </div> </form></body></html>
页:
[1]