Implementing Custom Web Controls and Custom Web User Controls
Sadly, all chapters must draw to an end, even though this one seems to be interminable. I have enjoyed writing it and think I could write a whole book on component building ”but there is much to do yet beyond component building. The question is how to bow out from this topic gracefully?
You are reading a book that suggests as a prerequisite a thorough understanding of object-oriented (OO) principles. Even if you haven't mastered OO principles prior to reading this book, you should have a good understanding of them after reading Chapter 9. I will try to offer some concise , sage wisdom on Web controls in the short space of a few paragraphs.
If you need a composite control, you want to implement a Web user control. A good resource for Web user controls is the IBUYSPY portal source code available from Microsoft for free. As a second resort you may elect to inherit from an existing Web control. Finally, your last resort should be to inherit from System.Web.UI.Control and build a custom Web control from scratch.
Probably the most important method when defining a custom Web control is the Render method, which receives an HtmlTextWriter object. You need to literally send HTML to the rendering page to define the custom control. Fortunately HtmlTextWriter contains several shared public fields and instance methods that facilitate rendering HTML using words rather than coarse HTML literal values. This helps significantly.
From my own experience and from conversations with Susan Warren at Microsoft I can tell you that it is a good idea to build composite controls with user controls because it is the easiest choice. If you do need to build a control from scratch, you will need to have a considerable understanding of HTML, caching, and the event model for ASP.NET Web pages. Sadly, again, this exceeds the charter of this book. If that is an area of great interest to you, I encourage you to pick up a copy of Developing Custom Controls for ASP.NET by Donny Mack and Doug Seven .