Asp.net| Control | sample | download
(i). overview
The example makes a TextBox control with the same functionality as the ASP.net textbox to understand
The implementation principle of control bottom
(ii). Code implementation
1. Core controls generate code files 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
11using System.Text;
12using System.Collections.Specialized;
13 13
14namespace Kingcontrols
15 15{
/**////<summary>
///Making a TextBox WebControl
///</summary>
public class Textbox:control, IPostBackDataHandler//ipostbackdatahandler: Handling postback data using
20 20 {
Public TextBox ()
22 22 {
23 23}
24 24
/**////<summary>
26 26///Set or get display text
///</summary>
public string Text
29 29 {
//web programming to use ViewState for two times back to share data
To get
32 32 {
A string s = (string) viewstate["Text"];
Return ((s = = null)? STRING.EMPTY:S);
35 35}
36 36
Panax Notoginseng Set
38 38 {
viewstate["Text" = value;
40 40}
41 41}
42 42
/**////<summary>
44 44///Generate render HTML formatting tags
///</summary>
///<param name= "Writer" ></param>
protected override void Render (HtmlTextWriter writer)
48 48 {
StringBuilder sb = new StringBuilder ();
SB. Append ("<input type=\" text\ "name=");
An SB. Append ("\" "+ UniqueID +" ""); identifiers, inheriting from the base class control
I'm a moron. Append ("value=");
53 53
//httputility.htmlencode converts user input strings into HTML format, primarily escaping the HTML keyword entered by the user as a non-HTML keyword character
SB. Append ("\" "+ Httputility.htmlencode (Text) +" "");
A sb. Append ("/>");
Writer. Write (sb.) ToString ());
58 58}
59 59
/**////<summary>
61 61///When postback, load new data entered by user
///</summary>
///<param name= "Postdatakey" ></param>
///<param name= "Postcollection" ></param>
The///<returns>true indicates data changes and will execute the following method raisepostdatachangedevent; Otherwise the data has not changed </returns>
The public virtual bool LoadPostData (string postdatakey, NameValueCollection postcollection)
67 67 {
Stroldvalue string = Text;
The string strnewvalue = Postcollection[this. UniqueID];
Stroldvalue = NULL | | (Stroldvalue!= null &&!stroldvalue.equals (strnewvalue)))
71 71 {
This is a. Text = Strnewvalue;
The return is true;
74 74}
return false;
76 76}
77 77
/**////<summary>
79 79///Only if the above method LoadPostData returns True, this method executes the
///</summary>
Bayi Bay I public virtual void raisepostdatachangedevent ()
82 82 {
OnTextChanged (Eventargs.empty);
84 84}
85 85
The public event EventHandler TextChanged;
protected virtual void ontextchanged (EventArgs e)
88 88 {
(TextChanged!= null)
90 90 {
TextChanged (this, e);
92 92}
93 93}
94 94}
95 95}
96 96
2. Front page File usingtextboxcontrol.aspx code (use method)
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
(iii). Sample code Download
Http://www.cnblogs.com/Files/ChengKing/KingControls.rar