• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

民工的家

Jackyli.918@hotmail.com
  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

HKH小类库系列(二)验证码控件实现,实现一拖即可

改是了网上流行验证码控件需要拷贝文件或手动配置httpHandlers的用法, 封装控件,拖动时自动写配置文件,简单方便。

在项目中用过向个验证码程序,有的需要拷贝页面,有的需要手动配置httpHandlers节点。

本人属于极懒性,既不想每次拷页面,也不想总是手动配置,SO封装此控件,从工具栏拖到页面即可使用。

图片生成部分,整合了几个程序的源码,大家最感兴趣的应该就是自动写配置文件这块了吧,

ControlDesigner是去掉拷贝页面和手动配置的关键点,它可以做为一个Attribute指明控件使用何种设计方式,并可以在某些设计时事件中做自己爱做的事情。

在本例中,我继承controlDesinger类,并对其GetDesignTimeHtml进行重写,以完成操作配置文件的功能。并通过以下配置使其应用到控件上。

[DesignerAttribute(typeof(VerifyCodeDesigner))]
    [ToolboxData("<{0}:VerifyCode runat=server></{0}:VerifyCode>")]
    public class VerifyCode : WebControl

 

代码
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web.Configuration;
using System.Web.UI.Design;

namespace HKH.WebControls
{
    
public class VerifyCodeDesigner : ControlDesigner
    {
        
public override string GetDesignTimeHtml()
        {
            
//向Web.Config自动注册httphandler
            IWebApplication webApp = (IWebApplication)Component.Site.GetService(typeof(IWebApplication));
            Configuration config 
= webApp.OpenWebConfiguration(false);

            HttpHandlersSection httpHandlersSection 
= (HttpHandlersSection)config.GetSection("system.web/httpHandlers");

            
bool hasHandler = false;
            
for (int i = 0; i < httpHandlersSection.Handlers.Count; i++)
            {
                
if (httpHandlersSection.Handlers[i].Path == "*.hkh")
                {
                    hasHandler 
= true;
                    
break;
                }
            }

            
if (!hasHandler)
            {
                httpHandlersSection.Handlers.Add(
new HttpHandlerAction("*.hkh", typeof(VerifyCodeHttpHander).AssemblyQualifiedName, "*", false));
                config.Save();
            }

            
return base.GetDesignTimeHtml();
        }
    }
}

 

 

大家都更好的想法一定要通知我哦,BS自己改进不共享的人。

 

 

框架下载地址:

HKH类库:/Files/Jackyli/HKHProjects.rar

 

posted on 2010-03-19 15:02  JackyLi  阅读(391)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3