(一). 概述
示例制作一个与asp.net textbox同样功能的 textbox control, 可以了解一下
control底层的实现原理
(二). 代码实现
1. 核心控件生成代码文件textbox.cs
1 1using system;
2 2using system.data;
3 3using system.configuration;
4 4using system.web;
5 5using system.web.security;
6 6using system.web.ui;
7 7using system.web.ui.webcontrols;
8 8using system.web.ui.webcontrols.webparts;
9 9using system.web.ui.htmlcontrols;
10 10
11 11using system.text;
12 12using system.collections.specialized;
13 13
14 14namespace kingcontrols
15 15{
16 16 /**//// <summary>
17 17 /// making a textbox webcontrol
18 18 /// </summary>
19 19 public class textbox : control, ipostbackdatahandler //ipostbackdatahandler: 处理回发数据使用
20 20 {
21 21 public textbox()
22 22 {
23 23 }
24 24
25 25 /**//// <summary>
26 26 /// 设置或获取显示文本
27 27 /// </summary>
28 28 public string text
29 29 {
30 30 //web编程中要用viewstate为两次回发共享数据
31 31 get
32 32 {
33 33 string s = (string)viewstate["text"];
34 34 return ((s == null) ? string.empty : s);
35 35 }
36 36
37 37 set
38 38 {
39 39 viewstate["text"] = value;
40 40 }
41 41 }
42 42
43 43 /**//// <summary>
44 44 /// 生成呈现html格式标记
45 45 /// </summary>
46 46 /// <param name="writer"></param>
47 47 protected override void render(htmltextwriter writer)
48 48 {
49 49 stringbuilder sb = new stringbuilder();
50 50 sb.append("<input type=/"text/" name=");
51 51 sb.append("/"" + uniqueid + "/""); //标识符,继承自基类control
52 52 sb.append("value=");
53 53
54 54 //httputility.htmlencode 将用户输入字串转换成html格式,主要转义用户输入的html关键字为非html关键字字符
55 55 sb.append("/"" + httputility.htmlencode(text) + "/"");
56 56 sb.append(" />");
57 57 writer.write(sb.tostring());
58 58 }
59 59
60 60 /**//// <summary>
61 61 /// 当回发时,装载用户输入的新数据
62 62 /// </summary>
63 63 /// <param name="postdatakey"></param>
64 64 /// <param name="postcollection"></param>
65 65 /// <returns>true表示数据改变,将会执行下面的方法raisepostdatachangedevent; 否则数据未改变</returns>
66 66 public virtual bool loadpostdata(string postdatakey, namevaluecollection postcollection)
67 67 {
68 68 string stroldvalue = text;
69 69 string strnewvalue = postcollection[this.uniqueid];
70 70 if( stroldvalue == null || ( stroldvalue != null && !stroldvalue.equals(strnewvalue)))
71 71 {
72 72 this.text = strnewvalue;
73 73 return true;
74 74 }
75 75 return false;
76 76 }
77 77
78 78 /**//// <summary>
79 79 /// 仅当上面方法loadpostdata返回true时,此方法将会执行
80 80 /// </summary>
81 81 public virtual void raisepostdatachangedevent()
82 82 {
83 83 ontextchanged(eventargs.empty);
84 84 }
85 85
86 86 public event eventhandler textchanged;
87 87 protected virtual void ontextchanged(eventargs e)
88 88 {
89 89 if (textchanged != null)
90 90 {
91 91 textchanged(this, e);
92 92 }
93 93 }
94 94 }
95 95}
96 96
2. 前台页面文件usingtextboxcontrol.aspx代码(使用方法)
1 <%@ page language="c#" autoeventwireup="true" codefile="usingtextboxcontrol.aspx.cs" inherits="_default" validaterequest="false"%>
2 <%@ register assembly="kingcontrols" namespace="kingcontrols" tagprefix="ksp" %>
3 … …
4 <ksp:textbox id="kingtextbox" runat="server" ontextchanged="kingtextbox_textchanged" text=""></ksp:textbox>
5 … …
6
(三). 示例代码下载
http://www.cnblogs.com/files/chengking/kingcontrols.rar
新闻热点
疑难解答
图片精选