QA


Q&A

Q1:

I want to create a text box that allows the user to enter only a certain type of input, such as numbers. How can I do this?

A1:

In the BMI calculator example we examined in the preceding hour, the user was prompted for his weight and height. Clearly, these inputs must be numeric ones. As we saw, if the user enters some input like Fred as his weight, it breaks the BMI calculator.

Therefore, you might think that an ideal solution would be to create a text box into which the user can enter only numbers. Such masked text boxes, as they are called, are rarely, if ever, used on web pages for a number of reasons.

First, while a masked text box would prevent the user from entering something like Fred as his weight, it would not prevent the user from entering nothing into the weight text box. Entering nothing will also break the BMI calculator.

Second, creating masked text boxes requires a bit of tricky client-side JavaScript programming. Users who have JavaScript disabled in their browser would therefore be able to enter any values into a masked text box.

Finally, and perhaps most importantly, users are not accustomed to masked text boxes on web pages. Therefore, the inclusion of masked text boxes would likely irritate users and lead them to conclude that your website was fundamentally different from the plethora of other websites they're used to.

As we will see in Hour 12, it is quite easy to ensure that a user's text box input conforms to a certain format. This is accomplished through validation Web controls, which we'll discuss in detail two hours from now.

(There are, however, masked input ASP.NET controls you can purchase. The nice thing about these third-party controls is that they take care of the tricky client-side JavaScript needed. You just drag and drop them onto your ASP.NET page, much like you would a regular TextBox. I've used and have been pleased with the masked input controls available at http://www.peterblum.com/.)

Q2:

Why does the MaxLength property not work with a MultiLine TextBox Web control?

A2:

Whereas a SingleLine or Password TextBox Web control is rendered as an <input> HTML element, MultiLine TextBoxes are rendered as the <textarea> HTML element. The HTML specification defines a maxlength attribute for <input> elements, but not for <textarea>.

Despite this limitation, it is still possible to restrict the number of characters entered into a MultiLine TextBox. In Hour 12 we'll examine one such workaround.




Sams Teach Yourself ASP. NET 2.0 in 24 Hours, Complete Starter Kit
Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
ISBN: 0672327384
EAN: 2147483647
Year: 2004
Pages: 233

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