(一). 概述
         示例制作一個與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
 
新聞熱點
疑難解答
圖片精選