TextBox control
A TextBox control, also known as a TextBox control, provides the user with the ability to enter text.
1. Property
The common properties and descriptions of the TextBox control are shown in table 1.
Table 1 Common Properties and descriptions of TextBox controls
Properties |
Description |
AutoPostBack |
Gets or sets a value that indicates whether automatic postback to the server occurs whenever the user presses the 〈enter〉 key or the 〈tab〉 key in a TextBox control |
CausesValidation |
Gets or sets a value that indicates whether validation is performed when the TextBox control is set to validate when a postback occurs |
Id |
Control ID |
Text |
The text to display for the control |
TextMode |
Gets or sets the behavior mode (single row, multiline, or password) of the TextBox control |
Width |
Width of the control |
Visible |
Whether the control is visible |
ReadOnly |
Gets or sets a value that indicates whether the contents of a TextBox control can be read-only |
CssClass |
Control-Rendered style |
BackColor |
The background color of the control |
Enabled |
Whether the control is available |
Most of the property settings of a TextBox control are similar to a label control, see the Label control property settings below, which mainly describes the TextMode property.
The TextMode property is primarily used to control the way text is displayed for a TextBox control, with the following 3 options for setting the property.
L single Row (Singleline): The user can enter information only on one line, and optionally limit the number of characters the control receives.
L Multi-line (MultiLine): Allows the user to enter multiple lines of text and perform line breaks when the text is very long.
L Password (Password): Masks the characters entered by the user with a black point () to hide the information.
For example, when verifying a user's login password, you can set the TextMode property of the TextBox control to password, which runs as shown in effect 1.
When you fill out a memo, the text can be a lot, and you can set the TextBox control's TextMode property to Multiline, which runs as shown in result 2.
Figure 1 Hide Login Password
Figure 2 Fill in the Notes information
2. Method
The common method of a TextBox control is similar to a Label control, and table 2 lists some of its common methods.
Table 2 Common methods and descriptions of TextBox controls
Method |
Description |
DataBind |
Binds a data source to the invoked server control and all its child controls |
Focus |
Set input focus for a control |
Dispose |
Enables the server control to perform the final cleanup operation before it is freed from memory |
Equals |
Determine if two object instances are equal |
The focus method of the Extbox control is primarily used to obtain its focus, for example, when a user writes the following code in a Page_Load event that contains a TextBox control page:
protected void Page_Load (object sender, EventArgs e)
{
Textbox1.focus ();
}
Run result 3 as shown.
Figure 3 Getting the TextBox control focus
3. Event
TextBox control common events are similar to label controls, see the Label control common events.
4. Example
Example:
TextBox Control Example
The following example compares the effect of a 3 TextBox control by setting a different TextMode property value. Create a new Web site, the default home page is default.aspx, add 3 TextBox controls on the Default.aspx page, and their properties are set as follows.
L Enter a TextBox control for the user name: The TextMode property is set to the Singleline,backcolor property of #ffe0c0 (light yellow), and the BorderColor property is Blue (blue).
L Enter the password for the TextBox control: TextMode property is set to password.
• TextBox control for entering memo information: The TextMode property is set to Multiline.
Execute the program and enter the text in 3 text boxes, as shown in the example run result 4.
Figure 4 TextBox Control Example
The complete code of the program is as follows:
★★★★★default.aspx.cs code file ★★★★★
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10
public partial class _default:system.web.ui.page
12 {
protected void Page_Load (object sender, EventArgs e)
14 {
15}
16
protected void textBox1_TextChanged (object sender, EventArgs e)
18 {
19}
20}
★★★★★default.aspx Design File ★★★★★
<%@ page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>textbox Control Example </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
User name: <asp:textbox id= "TextBox1" runat= "Server" backcolor= "#FFE0C0" bordercolor= "Blue"
Ontextchanged= "textBox1_TextChanged" ></asp:TextBox>
<br/>
Secret Code: <asp:textbox id= "TextBox2" runat= "Server" textmode= "Password" width= "149px" ></asp:TextBox>
<br/>
Preparation Note: <asp:textbox id= "TextBox3" runat= "Server" textmode= "MultiLine" ></asp:TextBox></div>
</form>
</body>
Pick Asp. NET standard control (TextBox control)