您的位置:首页 > 编程语言 > ASP

Asp.net 2.0 制作最原始的TextBox控件[一](示例代码下载)

2007-04-27 21:45 941 查看
(一). 概述
示例制作一个与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://files.cnblogs.com/ChengKing/KingControls.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐