最近自己写了一个自定义验证码控件把它拿出来和大家分享分享 具体步骤 1---》新建asp.net 网站
2---》添加新建项目 ,选择类库 3---》新建两个类 3.1--》自定义控件类(WebControl 派生类) 以下为引用的内容: using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace AuthCode { [ToolboxData("〈{0}:AuthCode runat=server>〈/{0}:AuthCode>")] public class AuthCode : WebControl { /// 〈summary> /// 获得验证码的值 /// 〈/summary> /// 〈returns>验证码〈/returns> public string GetValue() { return HttpContext.Current.Session["value"].ToString(); } [Bindable(true)] [Category("Appearance")] [Description("验证码字符长度")] [DefaultValue("ss")] [Localizable(true)] //长度 internal static int mySize; public int MySize { get { return AuthCode.mySize; } set { AuthCode.mySize = value; } } public AuthCode() : base(HtmlTextWriterTag.Img)//重写父类的构造(输出流的HTML标记) { } protected override void AddAttributesToRender(HtmlTextWriter writer) { base.AddAttributesToRender(writer);//将要输出的的HTML标签的属性和样式添加到指定的 HtmlTextWriter中 writer.AddStyleAttribute(HtmlTextWriterStyle.Cursor, "pointer");//添加样式 /**- * 图片的onclick事件 "this.src='VerifyImg.jd?id='+Math.random()" * 每次单击一次就有一个新的图片请求路径(VerifyImg.jd?id='+Math.random())参数只是 * 告诉浏览器这是一个新的请求然后经过 IHttpHander处理生成新的图片 id 没有任何实际意思(创造一个新的请求) * -**/ writer.AddAttribute("onclick", "this.src='img.jd?id='+Math.random()");//添加js VerifyImg.jd writer.AddAttribute(HtmlTextWriterAttribute.Src, "img.jd"); writer.AddAttribute("alt", "点击刷新"); }
} } 3.2--》新建处理类(必须实现 IHttpHandler,IRequiresSessionState 两个接口) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.UI.WebControls;
|