源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#设计模式之Template模板方法模式实现ASP.NET自定义控件 密码强度检测功能

  • 时间:2022-10-23 14:54 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#设计模式之Template模板方法模式实现ASP.NET自定义控件 密码强度检测功能
本文实例讲述了C#设计模式之Template模板方法模式实现ASP.NET自定义控件 密码强度检测功能。分享给大家供大家参考,具体如下: [b]一、理论定义[/b] 模板方法模式 预先定义实现了一些基本属性和方法,需要重新计算的部分,通过子类去重写 或  增加新方法来实现。 [b]二、应用举例[/b] 需求描述: ASP.NET自定义控件有很多通用的属性和事件, 通过继承System.Web.UI.WebControls.WebControl类,可以实现自定义控件。 WebControl拥有控件基本的方法和事件,让我们定义控件时,可以站在巨人的肩上, 避免重复造轮子。WebControl就相当于一个模板,改变模板的属性,或者往模板里面加东西,显示的内容就不一样。 密码强度检测的例子,是通过修改Strength 属性,来控制密码的强度。 [b]三、具体编码[/b] 1.一个 密码强度的枚举
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Com.Design.Gof.Template
{
  /// <summary>
  /// 密码强度枚举属性
  /// </summary>
  public enum StrengthOption
  {
    VeryLow=1,//很差
    Normer=2,//一般
    Good=3,//良好
    Perfect=4//非常棒,非常强,极佳
  }
}

2.密码强度 自定义控件
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;
[assembly: TagPrefix("Com.Design.Gof.Template", "asp")]
namespace Com.Design.Gof.Template
{
  [DefaultProperty("Text")]
  [ToolboxData("<{0}:PasswdStrength runat=server></{0}:PasswdStrength>")]
  public class PasswdStrength : WebControl
  {
    /// <summary>
    /// 当前密码强度
    /// </summary>
    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue(StrengthOption.VeryLow)]
    [Localizable(true)]
    public StrengthOption Strength
    {
      get
      {
        object bag = ViewState["StrengthOption"];
        if (bag == null) {
          return StrengthOption.VeryLow;
        }
        return (StrengthOption)ViewState["StrengthOption"];
      }
      set
      {
        ViewState["StrengthOption"] = value;
      }
    }
    protected override void RenderContents(HtmlTextWriter output)
    {
      string css = "";
      switch (Strength) {
        case StrengthOption.VeryLow: css = "bg1"; break;
        case StrengthOption.Normer: css = "bg2"; break;
        case StrengthOption.Good: css = "bg3"; break;
        case StrengthOption.Perfect: css = "bg4"; break;
        default: break;
      }
      output.Write("<div class='" + css + "'></div>");
    }
  }
}

3.ASPX页面调用控件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Template_PasswdStrength.aspx.cs" Inherits="Com.Design.Gof.Test.Web.Template_PasswdStrength" %>
<%@ Register Assembly="Com.Design.Gof" Namespace="Com.Design.Gof.Template" TagPrefix="asp" %>
<!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>
  <title></title>
  <style type="text/css">
div{width: 180px; height: 7px; border-left: 1px solid rgb(255, 117, 6); margin-left: 5px; margin-top:10px}
div.bg1{background: url("/images/pwd.png") no-repeat scroll 100% 0% transparent; }
div.bg2{background: url("/images/pwd.png") no-repeat scroll 100% 32% transparent; }
div.bg3{background: url("/images/pwd.png") no-repeat scroll 100% 65% transparent; }
div.bg4{background: url("/images/pwd.png") no-repeat scroll 100% 100% transparent; }
  </style>
</head>
<body>
 <h3>密码强度四种情况,Strength是asp:PasswdStrength的控件的自定义属性</h3>
  <p>非常弱</p>
  <asp:PasswdStrength ID="PasswdStrength1" runat="server" />
   <p>一般</p>
  <asp:PasswdStrength Strength=Normer ID="PasswdStrength2" runat="server" />
   <p>良好</p>
  <asp:PasswdStrength Strength=Good ID="PasswdStrength3" runat="server" />
   <p>很强</p>
  <asp:PasswdStrength Strength=Perfect ID="PasswdStrength4" runat="server" />
</body>
</html>

4.运行结果 [img]http://files.jb51.net/file_images/article/201709/2017914115551462.png?2017814115613[/img] 5.总结 自定义控件知识 附件里面包括了程序源码。也包括其他项目的测试,有控制台,有web。 此模式用Com.Design.Gof.Test.Web测试。 [b]附:[/b]完整实例代码点击此处[url=http://xiazai.jb51.net/201709/yuanma/CSharp-Com-Design-Gof-password-chk-codes(jb51.net).rar][b]本站下载[/b][/url]。 [b]PS:这里再为大家提供两款相关在线工具供大家参考使用:[/b] [b]密码安全性在线检测: [/b][url=http://tools.jb51.net/password/my_password_safe]http://tools.jb51.net/password/my_password_safe[/url] [b]高强度密码生成器: [/b][url=http://tools.jb51.net/password/CreateStrongPassword]http://tools.jb51.net/password/CreateStrongPassword[/url] [b]在线随机数字/字符串生成工具: [/b][url=http://tools.jb51.net/aideddesign/suijishu]http://tools.jb51.net/aideddesign/suijishu[/url] 更多关于C#相关内容还可查看本站专题:《[url=http://www.1sucai.cn/Special/116.htm]C#数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/611.htm]C#窗体操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/165.htm]C#常见控件用法教程[/url]》、《[url=http://www.1sucai.cn/Special/125.htm]WinForm控件用法总结[/url]》、《[url=http://www.1sucai.cn/Special/265.htm]C#数组操作技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/478.htm]C#面向对象程序设计入门教程[/url]》 希望本文所述对大家C#程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部