BCA Sem 5 | Unit 1 | ASP.NET Standard Controls
TextBox Control in ASP.NET
Single-line, Multi-line & Password — the most used input control in every web form
asp:TextBox makes it powerful with server-side access, validation, and 3 different display modes.
In plain HTML, a text input is written as <input type="text">. In ASP.NET, we use <asp:TextBox> with runat="server" — which means you can read, write, and control it from your C# code-behind.
The basic syntax is:
The most important property of a TextBox is TextMode — it controls how the TextBox looks and behaves:
Single Line TextBox
Default mode. A single row input box — used for names, email, phone numbers etc.
Multi Line TextBox
A text area — multiple rows of input. Used for messages, addresses, comments, feedback.
Password TextBox
Hides the typed characters — shows dots/asterisks instead. Used for passwords.
TextMode="SingleLine" is the default — if you don't write it, ASP.NET uses SingleLine automatically. You only need to set TextMode when you want MultiLine or Password.
Sample Registration Form using TextBox Controls
| Property | What It Does | Example Value |
|---|---|---|
ID | Unique name to access the TextBox in C# | "txtName" |
Text | The value inside the TextBox (read/set from code) | "Hello World" |
TextMode | Type of TextBox — SingleLine, MultiLine, Password | "Password" |
placeholder | Hint text shown when TextBox is empty | "Enter your name" |
MaxLength | Maximum number of characters allowed | "50", "100" |
Rows | Number of visible rows (MultiLine only) | "5", "10" |
Columns | Width in characters (MultiLine only) | "40", "60" |
ReadOnly | User can see but cannot change the text | "true" / "false" |
Enabled | Enable or disable the TextBox completely | "true" / "false" |
Visible | Show or hide the TextBox on the page | "true" / "false" |
AutoPostBack | Auto-submits page when text changes (triggers TextChanged) | "true" / "false" |
OnTextChanged | Event handler called when text is changed (with AutoPostBack) | "txt_TextChanged" |
Width | Visual width of the TextBox | "250px", "100%" |
Height | Visual height (mainly for MultiLine) | "100px" |
CssClass | Apply a CSS class for styling | "form-control" |
BackColor | Background color of TextBox | "LightYellow" |
ForeColor | Text color inside the TextBox | "DarkBlue" |
Font-Size | Font size of the text inside | "14px" |
BorderStyle | Border style of the TextBox | "Solid", "Dashed" |
ToolTip | Text shown on mouse hover | "Type your name here" |
TabIndex | Tab key order on the form | "1", "2", "3" |
Wrap | Word wrap in MultiLine TextBox | "true" / "false" |
1. SingleLine TextBox (Default):
2. MultiLine TextBox (Text Area):
3. Password TextBox:
After the user types something and submits the form, you read the value using the .Text property:
Setting AutoPostBack="true" makes the page automatically post back to the server when the user moves focus away from the TextBox — useful for live search or auto-fill features.
AutoPostBack="true" carefully — it causes a full page reload every time the user leaves the TextBox. For better user experience in modern apps, use AJAX UpdatePanel to avoid full page reloads.
.aspx file:
.aspx.cs file (C# Code-Behind):
asp:TextBoxis used to accept text input from the user.- Must have
runat="server"to access it from C# code-behind. - 3 TextModes:
SingleLine(default),MultiLine(textarea),Password(hidden input). Textproperty is used to read or set the value of a TextBox.MaxLengthlimits the number of characters the user can type.RowsandColumnsset size of MultiLine TextBox.ReadOnly="true"— user can see but not edit the text.AutoPostBack="true"triggers TextChanged event when user leaves the TextBox.placeholdershows hint text when TextBox is empty.Focus()method puts the cursor inside the TextBox automatically.Trim()is used to remove extra spaces from user input.