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.
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.
|
|