Adding Functionality


As cool as it can be to create and consume a custom server control in a very few lines of code (including both the control and the Web Forms page that consumes it), the control you just created provides minimal functionality. Clearly, for this control to be truly useful, you need to add to the control.

The next several sections will look at how to add properties and methods to a control; create, raise and handle events; handle postbacks; maintain state in a control; and create templated controls. By the end of this section, you’ll be able to add a significant amount of functionality to what is now a very simple control.

Adding Properties and Methods

To make the control more functional, you’re going to provide a property that determines the type of data entered into the text box. You’ll also define a method to format text entered into the text box based on this property.

Let’s start with adding a property by following these steps.

start example

Add a property

  1. Open TextBoxPlus.vb in the Chapter_10_Controls project.

  2. Add the following code to the top of the file, just below the Imports statements. The code creates an enumeration that defines the allowable values of the property you’re going to add:

    Public Enum TextTypes CurrencyText DecimalText PlainText End Enum
  3. Copy the _labelText variable definition and paste a copy below the original. Modify the variable name of the copy to _textType, change the type from String to TextTypes, and then set its default value to TextTypes.PlainText. Using TextTypes as the datatype will restrict the allowable values for this property to those defined by the TextTypes enumeration:

    Dim _textType As TextTypes = TextTypes.PlainText
  4. Copy the LabelText Property procedure and paste a copy below the original. Change the DefaultValue attribute of this property to TextTypes.PlainText, and then modify the property procedure so that it looks like the following code. (The attributes have been omitted for clarity.)

    Property TextType() As TextTypes Get Return _textType End Get Set(ByVal Value As TextTypes) _textType = Value End Set End Property

  5. Save the file, but don’t close it yet.

end example

Note

Contrary to what you might think, the DefaultValue attribute you set in Step 4 does not actually initialize the value of the variable it is applied to. This attribute is actually intended to allow developers to query the metadata for a component to determine the default value of a property programmatically, and reset it to that value if desired. Thus, if you want the value of a variable to be initialized to a particular value, you should do so by initializing the value of the private member associated with the property, as shown in Step 3 of the preceding example. You can findout more about this attribute at http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemcomponentmodeldefaultvalueattributeclasstopic.asp and http://support.microsoft.com/default.aspx?scid=kb;en-us;Q311339.

Now let’s look at adding a method.

start example

Add a method

  1. Add the following code directly below the End Sub of the Render method:

    Protected Sub FormatText() Select Case _textType Case TextTypes.CurrencyText Me.Text = FormatCurrency(Me.Text) Case TextTypes.DecimalText Me.Text = Format(Convert.ToInt32(Me.Text), "F") End Select End Sub

    Using the FormatCurrency and Format methods ensures that the currency and decimal formatting will work for multiple locales, based on the LCID attribute of the @ Page directive.

  2. Add the following code to the Render method, before the call to MyBase.Render:

    If Page.IsPostBack Then If _textType <> TextTypes.PlainText Then FormatText() End If End If

    Checking the IsPostBack method prevents the text property from being formatted at design time, since the Render method of the control is called at design time each time a property of the control is altered, in order to display the design-time rendering of the control.

  3. Save the class file and rebuild the project.

  4. Add a new Web Form to the Chapter_10 project. (Not the Chapter_10_ Controls project.) Name it TBP_Client.aspx.

  5. Change the pageLayout property of the page to FlowLayout.

  6. Using the Toolbox, add an instance of the TextBoxPlus control to the page, and then add a Button control to the page.

  7. Set the TextType property of the TextBoxPlus control to CurrencyText, and the LabelText property to Enter a whole number:. (Note: If you don’t see the TextType property listed, then, in the Solution Explorer, expand the References node under the Chapter_10 project and delete the reference for Chapter_10_Controls. Delete the TextBoxPlus control you just added to the form and add a fresh one. The reference for Chapter_10_Controls will be updated within the project.)

  8. Save TBP_Client.aspx, build the project, and then browse the page. The output should look similar to the following illustration.

    click to expand

  9. Enter 250 in the text box, and then click the button. The resulting output should look similar to the following illustration.

    click to expand

    If you add the LCID attribute to the @ Page directive, and set its value to that of a country other than the United States (such as 2057 for the United Kingdom), the output of the TextBoxPlus control will automatically reflect the change, with no changes to the control necessary.

end example




Microsoft ASP. NET Programming with Microsoft Visual Basic. NET Version 2003 Step by Step
Microsoft ASP.NET Programming with Microsoft Visual Basic .NET Version 2003 Step By Step
ISBN: 0735619344
EAN: 2147483647
Year: 2005
Pages: 126

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