asp.net|控制項|樣本|下載
(一). 概述
樣本製作一個與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