codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



Home » ASP » Article

ASP.NET : The textbox control

Article by: David Sussman, et al (7/8/2002)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Summary: This server control is ASP.NET's version of the HTML textbox form control. In fact, it doubles up and also provides the functionality of the HTML <textarea> form control.
Viewed: 176319 times Rating (15 votes): 
 4.3 out of 5
 Rate this Article  Read Comments  Post Comments


Previous Page  Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 Page 9  Page 11 Page 12 Page 13 Next Page  

The <asp:textbox> Control

This server control is ASP.NET's versionof the HTML textbox form control. In fact, it doubles up and also provides the functionality of the HTML <textarea> form control. Text areas are simply text boxes that feature multiple lines, thus allowing you to input larger quantities of text. The textbox control is also able to supply the functionality of an HTML form password control.

 

To be able to cover the remit of three HTML form controls, the <asp:textbox> control needs some extra attributes:

 

        textmode specifies whether you want the control to have one line (this is the default, so we don't need to set it if we simply want one line), many lines (set it to multiline), or have a single line of masked content (set it to password if you want to conceal the text that's been entered).

        rows specifies the number of rows you want the textbox to have and will only work if textmode is set to multiple.

        columns specifies the number of columns you want the textbox to have. It will only work if textmode is set to multiple.

 

If you wish to provide any default text that appears in the control, you can either place it between the opening and closing tags:

 

<asp:textbox id="text1" runat="server">Default text here...</asp:textbox>

 

or set it in the text attribute:

 

<asp:textbox id="text1" runat="server" text="Default text here..."/>


Now we'll create a short example that uses the textbox control to ask for the name and address of the user, and a password as well. In HTML, this would require three different types of control: here we shall only use the one <asp:textbox> control.

Try It Out Using the <asp:textbox> Control

1.       Time to start your web page editor again and type in the following:

 

<script runat="server" language="C#">

void Page_Load()

{

Message1.Text = "";

Message2.Text = "";

Message3.Text = "";

 

if (text1.Text != "") {

Message1.Text = "You have entered the following name: " + text1.Text;

}

 

if (text2.Text != "") {

Message2.Text = "You have entered the following address: " + text2.Text;

}

 

if (text3.Text != "") {

Message3.Text = "You have entered the following password: " + text3.Text;

}

}

</script>

<html>

<head>

<title>Text Box Example</title>

</head>

<body>

<asp:label id="Message1" runat="server" />

<br />

<asp:label id="Message2" runat="server" />

<br />

<asp:label id="Message3" runat="server" />

<br />

<form runat="server">

Please enter your name:

<asp:textbox id="text1" runat="server" />

<br /><br />

Please enter your address:

<asp:textbox id="text2" runat="server" rows=5 textmode="multiline" />

<br /><br />

Please enter your chosen password:

<asp:textbox id="text3" runat="server" textmode="password" />

<br /><br />

<input type="Submit">

</form>

</body>

</html>

 

2.       Save this as textboxpage.aspx.


 

3.       Open textboxpage.aspx in your browser, and type in some details:

 

 

4.       Click on Submit Query to see the results:

 


 

How It Works

Within the form, we have created three types of textbox control:

 

<asp:textbox id="text1" runat="server" />

<br /><br />

Please enter your address:

<asp:textbox id="text2" runat="server" rows=5 textmode="multiline" />

<br /><br />

Please enter your chosen password:

<asp:textbox id="text3" runat="server" textmode="password" />

 

The first texbox is identified as text1, and requires no other attributes other than id and runat. This is displayed as a single text field. The second control, text2, is a multiline textbox (which will render as a text area), and requires that we set the textmode attribute to multiline, so that we can set the number of rows we wish this textbox to have. Here, we have set it to five for the address. Lastly, we create a third control, text3, which we set to password with the textmode attribute. This, again, will display a single line text field, but any text typed into it is obscured by asterisks.

 

To display the results from three sets of controls, we have used three separate <asp:label> controls:

 

<asp:label id="Message1" runat="server" />

<br />

<asp:label id="Message2" runat="server" />

<br />

<asp:label id="Message3" runat="server" />

 

Each one of these is identified with a different id attribute. In this way, we can pass information from each of our three textboxes to a separate label control. The job of assigning text values to these three label controls falls to the ASP.NET code contained within <script> tags at the top of the page.

 

The code that assigns the values is very repetitive:

 

Message1.Text = "";

Message2.Text = "";

Message3.Text = "";

 

if (text1.Text != "") {

Message1.Text = "You have entered the following name: " + text1.Text;

}

 

if (text2.Text != "") {

Message2.Text = "You have entered the following address: " + text2.Text;

}

 

if (text3.Text != "") {

Message3.Text = "You have entered the following password: " + text3.Text;

}


 

First we make sure blank values are assigned to each of the <asp:label> controls in the first three lines. This is because once the page has been posted back, it will display the old messages, unless we clear them.

 

Then for the first control Message1, we take the text information, and assign it to Message1's text attribute. This will display the name information. For the second control, we take the text information from the second textbox control, and assign it to the text attribute, and we do likewise for the third. Note that the line breaks from the multi-line control (second textbox control) won't display in the label on the result page. Next, we place each statement within an if statement. We will cover how they work in Chapter 6, but for the time being, just think of them being used to check each control individually and decide on whether to display a message depending on if the user has supplied some contents or not. So if you only entered information into the name field, only one message would be displayed. Go ahead, try it and see.

Previous Page  Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 Page 9  Page 11 Page 12 Page 13 Next Page  




Click here to Buy!

Buy Beginning ASP.NET with C# here

© Copyright 2002 Wrox Press This chapter is written by David Sussman, et al and taken from "Beginning ASP.NET with C#" published by Wrox Press Limited in June 2002; ISBN 1861007345; copyright Wrox Press Limited 2002; all rights reserved.

No part of these chapters may be reproduced, stored in a retrieval system or transmitted in any form or by any means -- electronic, electrostatic, mechanical, photocopying, recording or otherwise -- without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.






CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'ASP.NET : The textbox control'
RELATED ARTICLES
ASP FilesystemObject
by Jeff Anderson
An introduction to the Filesystemobject
ASP GetTempName
by Jeff Anderson
Use the GetTempName method to create a randomly generated temporary file on the server.
ASP Format Date and Time Script
by Jeff Anderson
An ASP script showing the variety of date and time formats possible using the FormatDateTime Function.
ASP OpenTextFile
by Jeff Anderson
An introduction to the OpenTextFile Method of the FileSystemObject
Email validation using Regular Expression
by Jeff Anderson
Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP.
Add or Subtract Hours in SQL or ASP using DateAdd
by Jeff Anderson
A beginners guide to using the SQL DATEADD function to add or subtract hours. Particularly useful when setting the time displayed on the ASP page to a different time zone (eg when the server is in the US, and the site is for a UK audience).
The asp:radiobutton and asp:radiobuttonlist control
by David Sussman, et al
In HTML, radio buttons are used when we need to make multiple sets of choices available, but we want the user to select only one of them.
The asp:checkbox and asp:checkboxlist control
by David Sussman, et al
Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons.
ASP FileExists
by Jeff Anderson
An introduction to the FileExistsMethod of the FileSystemObject
Concatenate strings in sql
by Jeff Anderson
A brief introduction to concatenating strings in an sql query (using SQL server or access databases).








Recent Forum Threads
• Global.Mapper.v15.2.3.b060614
• Geometric_Glovius_Pro_v3.6.1
• VERO.SurfCAM.v2014
• Schlumberger.Petrel.V2013.2
• Petrel.V2013.2
• Altair.HyperWorks.v12
• VoluMill.v6.1
• VoluMill.NEXION.6
• VERO.SurfCAM.v2014


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2014