这篇文章主要介绍了C#设计模式之Template模板方法模式实现ASP.NET自定义控件 密码强度检测功能,简单介绍了模板方法模式的定义、原理及检测密码强度的相关使用技巧,需要的朋友可以参考下
正文
C#设计模式之Template模板方法模式实现ASP.NET自定义控件 密码强度检测功能
本文实例讲述了c#设计模式之template模板方法模式实现asp.net自定义控件 密码强度检测功能。分享给大家供大家参考,具体如下:
一、理论定义
模板方法模式 预先定义实现了一些基本属性和方法,需要重新计算的部分,通过子类去重写 或 增加新方法来实现。
二、应用举例
需求描述: asp.net自定义控件有很多通用的属性和事件, 通过继承system.web.ui.webcontrols.webcontrol类,可以实现自定义控件。
webcontrol拥有控件基本的方法和事件,让我们定义控件时,可以站在巨人的肩上,
避免重复造轮子。webcontrol就相当于一个模板,改变模板的属性,或者往模板里面加东西,显示的内容就不一样。
密码强度检测的例子,是通过修改strength 属性,来控制密码的强度。
三、具体编码
1.一个 密码强度的枚举
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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.密码强度 自定义控件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
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页面调用控件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<%@ 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.运行结果

5.总结
自定义控件知识
附件里面包括了程序源码。也包括其他项目的测试,有控制台,有web。
此模式用com.design.gof.test.web测试。
附:完整实例代码点击此处本站下载。
希望本文所述对大家c#程序设计有所帮助。
原文链接:http://www.cnblogs.com/HCCZX/archive/2012/08/06/2625424.html

发表评论