Adding Properties and Methods to Controls


Adding Properties and Methods to Controls

You can add properties and methods to a control in the same way you would add properties and methods to any Visual Basic class. If a property or method is declared with the Public modifier, you can access it from within an ASP.NET page.

Suppose that you want to create a control that displays text with different colors. The control in Listing 28.3 demonstrates how you can create a control with properties.

Listing 28.3 ShowColor.vb
 Imports System Imports System.Web Imports System.Web.UI Namespace myControls Public Class ShowColor: Inherits Control Public Text As String Public Color As String Protected Overrides Sub Render( objTextWriter As HtmlTextWriter )   objTextWriter.AddAttribute( "Color", Color )   objTextWriter.RenderBeginTag( "Font" )   objTextWriter.Write( Text )   objTextWriter.RenderEndTag() End Sub End Class End Namespace 

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

The ShowColor class in Listing 28.3 contains two public variables named Text and Color . In the Render method, the value of the Text variable is rendered with the value of the Color variable.

The page in Listing 28.4 illustrates how you can use the ShowColor class within an ASP.NET page.

Listing 28.4 DisplayShowColor.aspx
 <%@ Register TagPrefix="myControls" Namespace="myControls" Assembly="ShowColor"%> <html> <head><title>DisplayShowColor.aspx</title></head> <body> <myControls:ShowColor   Text="Hello World!"   Color="Red"   Runat="Server" /> </body> </html> 

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

In Listing 28.4, the properties of the ShowColor control are set when the control is declared. The ShowColor control displays the text Hello World! in the color red.

Instead of setting properties declaratively , you also can set control properties programmatically. For example, the page in Listing 28.5 sets the Text and Color properties in the Page_Load subroutine.

Listing 28.5 DisplayShowColor2.aspx
 <%@ Register TagPrefix="myControls" Namespace="myControls" Assembly="ShowColor"%> <Script Runat="Server"> Sub Page_Load   ctrlShowColor.Text = "Hello World!"   ctrlShowColor.Color = "Red" End Sub </Script> <html> <head><title>DisplayShowColor2.aspx</title></head> <body> <myControls:ShowColor   ID="ctrlShowColor"   Runat="Server" /> </body> </html> 

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

Using Property Accessor Functions

You can create properties by using property Get and Set accessor functions. The advantage of using accessor functions is that they enable you to execute additional programming logic when you read or set a property. For example, you can use accessor functions to perform data type conversions and handle errors.

The control in Listing 28.6 uses accessor functions for both the Text and Color properties. If you attempt to assign anything but Hello World! to the Text property, an ArgumentException is generated.

Listing 28.6 ShowColorAccessor.vb
 Imports System Imports System.Web Imports System.Web.UI Namespace myControls Public Class ShowColorAccessor: Inherits Control Private _text As String Private _color As String Public Property Text As String Get   Return _text End Get Set   If Value <> "Hello World!" Then     Throw New ArgumentException( "You didn't enter Hello World!" )   Else     _text = Value   End If End Set End Property Public Property Color As String Get   Return _color End Get Set   _color = Value End Set End Property Protected Overrides Sub Render( objTextWriter As HtmlTextWriter )   objTextWriter.AddAttribute( "Color", _color )   objTextWriter.RenderBeginTag( "Font" )   objTextWriter.Write( _Text )   objTextWriter.RenderEndTag() End Sub End Class End Namespace 

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

In the Set accessor function in Listing 28.6, an ArgumentException is raised if a Value other than Hello World! is passed to the function (see Figure 28.2).

Figure 28.2. Argument exception from ShowColorAccessor control.

graphics/28fig02.jpg

Using Methods with Controls

You can add a method to a control by declaring a function or subroutine with a Public modifier. For example, the control in Listing 28.7 has a method named Reverse . When this method is called, the contents of the Text property are reversed .

Listing 28.7 ReverseMethodControl.vb
 Imports System Imports System.Web Imports System.Web.UI Imports Microsoft.VisualBasic Namespace myControls Public Class ReverseMethodControl: Inherits Control Public Text As String Public Sub Reverse()   Text = StrReverse( Text ) End Sub Protected Overrides Sub Render( objTextWriter As HtmlTextWriter )   objTextWriter.Write( Text ) End Sub End Class End Namespace 

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

Notice that the Microsoft.VisualBasic namespace is imported. You need to import this namespace because the StrReverse method is a member of it. The StrReverse method reverses the text in a string.

The page in Listing 28.8 demonstrates how you can call the Reverse method from within an ASP.NET page.

Listing 28.8 DisplayReverseMethodControl.aspx
 <%@ Register TagPrefix="myControls" Namespace="myControls" Assembly="ReverseMethodControl"%> <Script Runat="Server"> Sub Page_Load   ctrlReverse.Text = "Hello World!"   ctrlReverse.Reverse() End Sub </Script> <html> <head><title>DisplayReverseMethodControl.aspx</title></head> <body> <myControls:ReverseMethodControl   ID="ctrlReverse"   Runat="Server" /> </body> </html> 

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

In Listing 28.8, the Text property of ReverseMethodControl is assigned the value Hello World! . Next, the Reverse method is called, and the value of the Text property is reversed. The control outputs this string:

 
 !dlroW olleH 


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