ASP.NET Custom Server Controls: Get in Control of ASP.NET - Understanding the custom control
(Page 5 of 5 )
In this section, we shall go into the detail about the custom control. First of all let us consider the following lines:
PublicClassLabelErrMsg
InheritsSystem.Web.UI.WebControls.WebControl
In the above sections, I explained that any custom control (or class of custom controls) needs to be derived either from System.Web.UI.Control or System.Web.UI.WebControls.WebControl. The above statement does exactly this. Since I would like to implement some features of UI automatically (like width, height, font and so on), I extended through the “WebControl” class instead of simply the “Control” class. Let us consider the following code fragment:
Dim_textAsString
<Bindable(True), Category("Appearance"), DefaultValue("")>Property[Text]()AsString
Get
Return_text
EndGet
Set(ByValValueAsString)
_text = Value
EndSet
EndProperty
I hope you can understand the above. It just defines a property named “Text” which gets displayed in the properties window under the category “Appearance”. When the user wants to change information in the label, he does it through this property. Now, we shall proceed further.
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
The above code fragment is the heart of our custom control. Every control (including “page”) has the “Render” method, which sends HTML, CSS, JavaScript, and so forth to the response stream (browser). This method gets executed by ASP.NET runtime (after every postback) separately for every control, including the “page” control.
Any comments, suggestions, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |