ASP.NET Custom Server Controls: Cute ASP.NET TextBox Control - Developing the “MyTextBox” Control
(Page 2 of 6 )
Now, we will develop a simple “Textbox” type of control right from scratch. I call it “MyTextBox” throughout this article. The following steps help you to create the custom control using Visual Studio.NET 2003.
Click start -> Programs -> Microsoft Visual Studio .NET 2003 ->Microsoft Visual Studio .NET 2003 to open the IDE (Integrated Development Environment).
Click File -> New -> Project.
Within the “new project” dialog box select “visual basic projects” as the project type, “Web Control Library” as the template and provide the name “SimpleCustomControl” along with the location you desire (Figure:1), and finally click “ok.”

Within the solution explorer, right-click on “WebCustomControl1.Vb” and select “delete” to delete it.
Go to the project menu and select “Add New Item.”
Within the “Add New Item” dialog box select “WebCustomControl” and name it “MyTextBox.vb.”
Modify code (or just copy and paste) in the editor as follows:
Imports System.ComponentModel
Imports System.Web.UI
<DefaultProperty("Text"), ToolboxData("<{0}:MyTextBox runat=server></{0}:MyTextBox>")> Public Class MyTextBox
Inherits System.Web.UI.WebControls.WebControl
Implements IPostBackDataHandler
Property [Text]() As String
Get
Return viewstate("text") & ""
End Get
Set(ByVal Value As String)
viewstate("text") = Value
End Set
End Property
Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
MyBase.AddAttributesToRender(output)
output.AddAttribute (HtmlTextWriterAttribute.Name, Me.UniqueID)
output.AddAttribute (HtmlTextWriterAttribute.Id, Me.ClientID)
output.AddAttribute (HtmlTextWriterAttribute.Type, "text")
output.AddAttribute (HtmlTextWriterAttribute.Value, Text)
output.RenderBeginTag (HtmlTextWriterTag.Input)
output.RenderEndTag()
End Sub
Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.
NameValueCollection) As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData
If Text <> postCollection(Me.UniqueID) Then
Text = postCollection(Me.UniqueID)
Return True
End If
Return False
End Function
Public Sub RaisePostDataChangedEvent() Implements System.Web.UI.IPostBackDataHandler.
RaisePostDataChangedEvent
End Sub
End Class
Next: Testing the control with a Web Application >>
More ASP.NET Articles
More By Jagadish Chaterjee