Prev | Current Page 218 | Next

Rob Cameron and Dale Michalk

"Pro ASP.NET 3.5 Server Controls and AJAX Components"

AddAttribute("value",Text);
Now that we have covered the basics of inheriting from WebControl, we can move on to add
style capabilities to our TextBox control from earlier in this book.
A Styled TextBox Control
In this section, we bring back our favorite TextBox control from chapters past and update it with
WebControl capabilities. As you would guess, most of the implementation remains the same.
The biggest changes relate to how we handle the rendering process.
CHAPTER 4 ?–  THE WEBCONTROL BASE C LASS AND CONTROL STYLES 137
The first step in updating TextBox is to inherit from WebControl and set the constructor to
create the tag for the outer shell of the control. The following code snippet sets up our WebControl
version of the TextBox to render an tag:
public class Textbox : WebControl, IPostBackDataHandler
{
public Textbox() : base(HtmlTextWriterTag.Input)
{
}
}
The rendering code in this version of TextBox is dramatically smaller than the previous
version that inherited from Control. All we have to implement is the AddAttributesToRender()
method to set the tag with the appropriate attributes, and we need to call the base class
version to add the style properties:
override protected void AddAttributesToRender(HtmlTextWriter writer)
{
writer.


Pages:
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230