ASP.NET Custom Server Controls: Get in Control of ASP.NET - Developing a simple custom control
(Page 3 of 5 )
To develop any custom control we generally design a class which gets derived from either of the following two classes:
- System.Web.UI.Control
- System.Web.UI.WebControls.WebControl
The System.Web.UI.Control provides only the basic functionality to make our custom control participate in the page frame work. The System.Web.UI.WebControls.WebControl is already derived from the System.Web.UI.Control class and provides support for additional properties such as font, height, width, and so on. In this article we will focus on working with the System.Web.UI.WebControls.WebControl class.
Now, we will develop a small “label” type of control from scratch. I call that “LabelErrMsg”, because it may be used simply to display some red colored message in a red bordered box. 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 project type, “Web Control Library” as template, and provide the name as “SimpleCustomControl” along with the location you desire (Figure 2) and finally click “ok”.

Figure 2
- 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 “LabelErrMsg.vb” as shown in Figure 3.

Figure 3
- Modify code in the editor as follows:
ImportsSystem.ComponentModel
ImportsSystem.Web.UI
<DefaultProperty("Text"), ToolboxData("<{0}:LabelErrMsg runat=server></{0}:LabelErrMsg>")>PublicClassLabelErrMsg
InheritsSystem.Web.UI.WebControls.WebControl
Dim_textAsString
<Bindable(True), Category("Appearance"), DefaultValue("")> _
Property[Text]()AsString
Get
Return_text
EndGet
Set(ByValValueAsString)
_text = Value
EndSet
EndProperty
ProtectedOverridesSubRender(ByValoutputAsSystem.Web.UI.HtmlTextWriter)
output.Write("<table border=1>")
output.Write("<tr><td bordercolor=red style='COLOR: red; BACKGROUND-COLOR: lightgoldenrodyellow'>")
output.Write([Text])
output.Write("</td></tr></table>")
EndSub
EndClass
- Press Shift+CtrI+B to build the solution.
Next: Testing the control with a web application >>
More ASP.NET Articles
More By Jagadish Chaterjee