JavaScript EditorJavascript debugger     Javascript examples

Team LiB
Previous Section Next Section

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.

Add a property

  1. Open TextBoxPlus.cs in the Chapter_10_Controls project.

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

    public enum TextType  CurrencyText DecimalText PlainTex }
  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:

    private TextTypes _textType = 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.)

    public TextTypes TextType  ge  return _textType  se  _textType = value  }
  5. Save the file, but don’t close it yet.


    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 find out more about this attribute at and;en-us;Q311339.

Now let’s look at adding a method.

Add a method

  1. Add the following code directly below the closing curly brace of the Render method:

    protected void FormatText(  switch(_textType  case TextTypes.CurrencyText this.Text=(double.Parse(this.Text)).ToString("C") break case TextTypes.DecimalText this.Text=(Convert.ToInt32(this.Text)).ToString("F") break  }

    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 base.Render:

    if ( Page.IsPostBack )  if ( _textType != TextTypes.PlainText   this.FormatText()  }

    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.

Team LiB
Previous Section Next Section

JavaScript EditorJavascript debugger     Javascript examples