C#設(shè)計模式之Template模板方法模式實現(xiàn)ASP.NET自定義控件 密碼強度檢測功能
本文實例講述了C#設(shè)計模式之Template模板方法模式實現(xiàn)ASP.NET自定義控件 密碼強度檢測功能。分享給大家供大家參考,具體如下:
一、理論定義
模板方法模式 預先定義實現(xiàn)了一些基本屬性和方法,需要重新計算的部分,通過子類去重寫 或 增加新方法來實現(xiàn)。
二、應用舉例
需求描述: ASP.NET自定義控件有很多通用的屬性和事件, 通過繼承System.Web.UI.WebControls.WebControl類,可以實現(xiàn)自定義控件。
WebControl擁有控件基本的方法和事件,讓我們定義控件時,可以站在巨人的肩上,
避免重復造輪子。WebControl就相當于一個模板,改變模板的屬性,或者往模板里面加東西,顯示的內(nèi)容就不一樣。
密碼強度檢測的例子,是通過修改Strength 屬性,來控制密碼的強度。
三、具體編碼
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頁面調(diào)用控件
<%@ 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.運行結(jié)果

5.總結(jié)
自定義控件知識
附件里面包括了程序源碼。也包括其他項目的測試,有控制臺,有web。
此模式用Com.Design.Gof.Test.Web測試。
附:完整實例代碼點擊此處本站下載。
PS:這里再為大家提供兩款相關(guān)在線工具供大家參考使用:
密碼安全性在線檢測:
http://tools.jb51.net/password/my_password_safe
高強度密碼生成器:
http://tools.jb51.net/password/CreateStrongPassword
在線隨機數(shù)字/字符串生成工具:
http://tools.jb51.net/aideddesign/suijishu
更多關(guān)于C#相關(guān)內(nèi)容還可查看本站專題:《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#窗體操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
.NET(C#):Emit創(chuàng)建異常處理的方法
.NET(C#):Emit創(chuàng)建異常處理的方法,需要的朋友可以參考一下2013-04-04

