Inheriting from Existing Controls


All the controls in this chapter are inherited from the base Control class. However, in some situations, inheriting from a different control makes more sense. You can inherit from any of the HTML or Web controls in the ASP.NET framework.

If you inherit from the WebControl class, for example, you can take advantage of all the formatting properties available in that class. The control in Listing 28.32 illustrates how you can inherit from the WebControl class.

Listing 28.32 myWebControl.vb
 Imports System Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Namespace myControls Public Class myWebControl Inherits WebControl Overrides Protected Sub RenderContents( objTextWriter As HtmlTextWriter )   objTextWriter.Write( "Hello World!" ) End Sub End Class End Namespace 

The C# version of this code can be found on the CD-ROM.

The control in Listing 28.32 inherits from the WebControl class. Notice that content is rendered within the RenderContents method instead of the standard Render method. By overriding the RenderContents method, you can take advantage of the WebControl class' built-in support for formatting.

The ASP.NET page in Listing 28.33, for example, displays the output of the myWebControl control in a bold red font with a yellow background.

Listing 28.33 DisplayWebControl.aspx
 <%@ Register TagPrefix="myControls" Namespace="myControls" Assembly="myWebControl"%> <html> <head><title>DisplayWebControl.aspx</title></head> <body> <form Runat="Server"> <myControls:myWebControl   BackColor="Yellow"   Font-Bold="True"   ForeColor="Red"   Runat="Server"/> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net