ASP.NET

ASP.NET Controls: The Text Box

A text box is a rectangular window that displays and/or request text from the user. Normally, the text box in ASP.NET follows the web rules established in…

3 min read664 words5 code samples

ASP.NET Controls: The Text Box

ASP.NET Controls: The Text Box#

Text Box Fundamentals#

Introduction#

A text box serves as a rectangular area for showing or requesting user input. Typically, an ASP.NET text box adheres to standard HTML input rules while introducing a few additional features. The System.Web.UI.WebControls namespace provides the TextBox class to support this functionality, and it is represented visually by a control of the same name.

Creating a Text Box#

If you are creating your web site using either Microsoft Visual Studio or Microsoft Visual Web Developer, to add a text box, from the Standard section of the Toolbox, drag a TextBox object and drop it on the form. To manually add a text box to a form, create an <asp:TextBox> tag. Here is an example:

C#
<%@ Page Language="C#" %>
<html>
<head>

<title>Exercise</title>
</head>
<body>

<form id="frmTimeSheet" runat="server">
  <asp:TextBox ID="txtFullName" runat="server"></asp:TextBox>
</form>

</body>
</html>

This would produce:

Characteristics of a Text Box#

The Text of a Text Box#

As the name implies, the primary purpose of a text box is to hold text. To have the control display text when it first appears, you can use the Properties window in a visual designer, find the Text property, and type your value. If you are coding manually, add the Text attribute to the tag and assign your desired string. Here is a demonstration:

C#
<%@ Page Language="C#" %>
<html>
<head>

<title>Exercise</title>
</head>
<body>

<form id="frmCalculation" runat="server">
  <asp:TextBox ID="txtNumber" Text="0.00" runat="server"></asp:TextBox>
</form>

</body>
</html>

To programmatically access the text of a text box, type the ID of the control, followed by a period, followed by Text. Here is an example:

C#
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">
private void ButtonClicked(object sender, EventArgs e)
{
    txtNumber.Text...;
}
</script>

<title>Exercise</title>
</head>
<body>

<form id="frmExercise" runat="server">
  <ul style="list-style-type: none;">
    <li><asp:TextBox ID="txtNumber"
		     Text="0.00"
		     runat="server"></asp:TextBox></li>
    <li><asp:Button id="btnExercise"
        	    Text="Exercise"
	      	    OnClick="ButtonClicked"
	      	    runat="server"></asp:Button></li>
  </ul>
</form>

</body>
</html>

To make the text box display a value, you can assign a value to it.

The Width of a Text Box#

When a text box has been added to a form, it assumes some default dimensions. The height is controlled by the operating system while the width is set in the .NET Framework. After some text has been added to the control, if the control is not width enough, some text would disappear on the right side. To control the width of a text box, you can specify it in terms of the width of a character. To support this, the <asp:TextBox> tag is equipped with an attribute named Columns. To set the width of a text box, assign the maximum number of characters to each line of text to this attribute. Here is an example:

C#
<%@ Page Language="C#" %>
<html>
<head>

<title>Exercise</title>
</head>
<body>

<form id="frmTimeSheet" runat="server">
  <asp:TextBox ID="txtAddress" Columns="32" runat="server"></asp:TextBox>
</form>

</body>
</html>

This would produce:

Changing the Value of a Text Box#

Interacting with a text box is as simple as clicking it and typing. You can also assign values to it through code. Whenever the content changes, the control triggers a TextChanged event. Since this is the default event, double-clicking the control in the designer will automatically generate the handler code. The event uses the EventArgs class and is useful for taking actions while the user is typing.

Auto-Completing a Text Box#

To assist the user with specifying the value of a text box, you can make it remember some of the values that were previously used on it. To support this, the <asp:TextBox> tag is equipped with an attribute named AutoCompleteType. This attribute uses a value of the AutoCompleteType enumeration. The members of this enumeration are: None, Disabled, Cellular, Company, Department, DisplayName, Email, FirstName, Gender, HomeCity, HomeCountryRegion, HomeFax, HomePhone, HomeState, HomeStreetAddress, HomeZipCode, Homepage, JobTitle, LastName, MiddleName, Notes, Office, Pager, BusinessCity, BusinessCountryRegion, BusinessFax, BusinessPhone, BusinessState, BusinessStreetAddress, BusinessUrl, BusinessZipCode, and Search. To visually specify this characteristic, on the form, click the text box. In the Properties window, click AutoCompleteType and select from the list. To manually specify this aspect, add an AutoCompleteType attribute to the <asp:TextBox> tag and assign the desired member of the enumeration to it.

Automatically Posting Back#

After using a text box, the user can click a button to send the value to you at the server. This characteristic is controlled by a Boolean attribute named AutoPostBack whose default value is "False". If you want the values to be automatically posted when the user clicks another control after using the text box, change the value of the AutoPostBack attribute from False to "True" to it. Here is an example:

C#
<%@ Page Language="C#" %>
<html>
<head>

<title>Exercise</title>
</head>
<body>

<form id="frmTimeSheet" runat="server">
  <asp:TextBox ID="txtFullName"
	       AutoPostBack="True"
	       runat="server"></asp:TextBox>
</form>

</body>
</html>

Home Copyright © 2009 C# Key