Asp.net 2.0 製作最原始的TextBox控件[一](示例代碼下載)

   
(一). 概述
         示例製作一個與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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章