Click here to Skip to main content
Click here to Skip to main content

Validated TextBox for ASP.NET

, 18 Mar 2004
Rate this:
Please Sign up or sign in to vote.
An article on how to make a composite custom control

Introduction

This webcontrol checks the user has entered a number with a certain range into the textbox. There is no need to draw also a required-validator and a range-validator on screen when type-checking is needed. It can also check a date, a string or an integer through the same mechanism. The purpose of this article is to learn and show how easy it is to wrap some code into a custom control which can be used elsewhere.

Background

The ValidatedTextBox is born out of the need for many textboxes, which contains money-values. Type-checking the money values, I used a range-validator. After putting a large number of textboxes and range-validators on my screen I realized I could make a custom control for it. Maybe I’ll extend the control in the future with client-side scripting so that a user can only press a number in the textbox.

Using the code

The control with class name ValidatedTextBox is a composite control, which consists of a textbox, a range-validator and optionally a required-validator. These three will be put after another in code. This is not so very complicated so I will only discuss three properties here: IsRequired, IsErrorTextBelow, MaxValue/Minvalue and a little intelligence: Aligning to the right when the textbox will contain a currency value.

The IsRequired property sets the required validator so that it will be rendered.

There is also a property IsErrorTextBelow that can be unset to place the errortext to the right of the textbox.

The maximum length of characters entered into a normal textbox can be set with the MaxLength property. The ValidatedTextBox control has properties MinValue and MaxValue to check the boundaries of the entered money value. These values are used to calculate the MaxLength. This way the control should pass the monkeytest.

The text aligns to the right when the Type property is currency. I was experimenting with this and did not know how to align text in a textbox to the right not using cascading style sheets. Therefore I tried overriding the render method and put a <div> around the textbox with aligning right. Now I know it can be easier, to set the style property of the textbox like:

if (rangeValidator.Type == ValidationDataType.Currency)
        textBox.Style["TEXT-ALIGN"] = TextAlign.Right.ToString();

Points of Interest

As I was mentioning before, I did not need to override the render function. I wanted to align text to the right but I did not find a property style for a textbox. I thought that Microsoft forgot to implement this. I checked the Text Field control from the HTML tab in Visual Studio and I was surprised to find a style property that handles aligning. Because a TextBox server control renders as an HTML input control, I thought that overriding the render method and appending: ”TEXT-ALIGN: right” to the style property would solve my problems. Fortunately, the textbox does have a style property but only at runtime.

Another property of textbox I want to mention here is MaxLength. This is perfect for catching too much characters entered. I totally did not know about this property and I think it is very handy. Validation on client-side does not have to be javascripting.

History

  • Version 1.0, March 2004.
  • Updated demo, 19 Mar 2004

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dries de Groot
Software Developer
Netherlands Netherlands
Hi my name is Dries de Groot. I have a Masters degree in applied physics and am graduated in solid-state matter. Besides that I am a .NET Professional with the following titles: MCAD, MCSD

Comments and Discussions

 
GeneralMy vote of 5 Pinmembermanoj kumar choubey7-Feb-12 19:30 
Generalabout controls Pinmemberavding8-Jun-06 22:35 
Generalsomthing similar Pinmemberhlaford21-Apr-04 11:25 
GeneralRe: somthing similar PinmemberDrieske22-Apr-04 20:37 
GeneralRe: somthing similar Pinmemberhlaford23-Apr-04 4:15 
GeneralExcellent! This is what I reaaly need! Pinmemberuydo16-Mar-04 6:23 
GeneralRe: Excellent! This is what I reaaly need! Pinmembervikramk24-Mar-04 3:30 
GeneralNot working sample PinsussASPNetMaster9-Mar-04 5:42 
GeneralRe: Not working sample PinmemberDrieske17-Mar-04 22:05 
GeneralAlrighty Then Pinmemberstrobosch9-Mar-04 1:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 19 Mar 2004
Article Copyright 2004 by Dries de Groot
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid