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
Sponsored by: FindMyHosting - Web Hosting Search
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: 73836 times Rating (7 votes): 
 4.9 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 version of 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'
Posted by :  Archive Import (kristine) at 11:37 on Friday, October 18, 2002
im looking for an article about dropdownlist but this link pointed me on a textbox. Is there other link for dropdownlist? I just want to know if i can assign how many columns in a dropdownlist.

Thanks!!!


To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
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.
Creating a Dynamic Reports using ASP and Excel
by Jeff Anderson
A simple way to generate Excel reports from a database using Excel.
Create an ASP SQL Stored Procedure
by Jeff Anderson
A beginners guide to setting up a stored procedure in SQL server and calling it from an ASP page.
ASP Shopping Cart
by CodeToad Plus!
Complete source code and demo database(Access, though SQL compatible) to an ASP database driven e-commerce shopping basket, taking the user through from product selection to checkout. Available to CodeToad Plus! Members
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.
Creating an SQL Trigger
by Jeff Anderson
A beginners guide to creating a Trigger in SQL Server
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.NET Forum Source Code
by ITCN
Complete open source website Forum and Discussion Board programmed in Microsoft dot Net 1.1 Framework with Visual Basic.
The asp:listbox control
by David Sussman, et al
The next HTML server control that we'll look at, <asp:listbox>, is very much related to <asp:dropdownlist>.
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
•  Need help for writing javascript code global empty fields validation
•  Help me to write a C++ program
•  begginer error
•  Re: Converting VC++6 to VC++8, Wrong Library Files
•  Re: Grep or fgrep ??
•  php on two apache vurtualhost sites
•  Dynamically use a text file
•  Text formatting in Perl
•  Re: dynamic crystal report generation


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006