ASP.NET TextBox Control – SingleLine, MultiLine, Password | BCA Sem 5 BKNMU



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

BKNMU Junagadh NEP 2020 Web Forms asp:TextBox Standard Controls
In simple words: A TextBox is a box on a web page where the user can type and enter text — like your name, email, password, or a long message. It is the most commonly used input control in any web form. ASP.NET's asp:TextBox makes it powerful with server-side access, validation, and 3 different display modes.
📖 What is asp:TextBox?

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:

<asp:TextBox ID="txtName" runat="server" TextMode="SingleLine" placeholder="Enter your name" />
🎨 3 Types of TextBox — TextMode Property

The most important property of a TextBox is TextMode — it controls how the TextBox looks and behaves:

SingleLine

Single Line TextBox

Default mode. A single row input box — used for names, email, phone numbers etc.

MultiLine

Multi Line TextBox

A text area — multiple rows of input. Used for messages, addresses, comments, feedback.

Password

Password TextBox

Hides the typed characters — shows dots/asterisks instead. Used for passwords.

ℹ️ Remember: 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.
🖥️ What a Real TextBox Form Looks Like

Sample Registration Form using TextBox Controls

Max 500 characters
📋 All Important Properties of asp:TextBox
PropertyWhat It DoesExample Value
IDUnique name to access the TextBox in C#"txtName"
TextThe value inside the TextBox (read/set from code)"Hello World"
TextModeType of TextBox — SingleLine, MultiLine, Password"Password"
placeholderHint text shown when TextBox is empty"Enter your name"
MaxLengthMaximum number of characters allowed"50", "100"
RowsNumber of visible rows (MultiLine only)"5", "10"
ColumnsWidth in characters (MultiLine only)"40", "60"
ReadOnlyUser can see but cannot change the text"true" / "false"
EnabledEnable or disable the TextBox completely"true" / "false"
VisibleShow or hide the TextBox on the page"true" / "false"
AutoPostBackAuto-submits page when text changes (triggers TextChanged)"true" / "false"
OnTextChangedEvent handler called when text is changed (with AutoPostBack)"txt_TextChanged"
WidthVisual width of the TextBox"250px", "100%"
HeightVisual height (mainly for MultiLine)"100px"
CssClassApply a CSS class for styling"form-control"
BackColorBackground color of TextBox"LightYellow"
ForeColorText color inside the TextBox"DarkBlue"
Font-SizeFont size of the text inside"14px"
BorderStyleBorder style of the TextBox"Solid", "Dashed"
ToolTipText shown on mouse hover"Type your name here"
TabIndexTab key order on the form"1", "2", "3"
WrapWord wrap in MultiLine TextBox"true" / "false"
💻 Syntax for All 3 TextBox Types

1. SingleLine TextBox (Default):

<!-- Simple text input --> <asp:TextBox ID="txtName" runat="server" TextMode="SingleLine" placeholder="Enter your name" MaxLength="50" Width="280px" />

2. MultiLine TextBox (Text Area):

<!-- Multi-line text area --> <asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine" Rows="5" Columns="40" placeholder="Enter your address..." MaxLength="500" Wrap="true" />

3. Password TextBox:

<!-- Password input - hides characters --> <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" placeholder="Enter password" MaxLength="20" Width="280px" />
⚙️ Reading TextBox Value in C# Code-Behind

After the user types something and submits the form, you read the value using the .Text property:

protected void btnSubmit_Click(object sender, EventArgs e) { // Read values from TextBoxes string name = txtName.Text; string address = txtAddress.Text; string pass = txtPassword.Text; // Check if name is empty if (name == "") { lblMsg.Text = "Please enter your name!"; } else { lblMsg.Text = "Welcome, " + name + "!"; } }
// Set a value into TextBox from code (e.g. on Page_Load) protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Pre-fill the TextBox with a default value txtName.Text = "Raj Patel"; txtName.Enabled = true; txtName.Focus(); // Put cursor in this TextBox on page load } }
🔄 AutoPostBack — TextBox with Auto-Submit

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.

<!-- TextBox with AutoPostBack --> <asp:TextBox ID="txtSearch" runat="server" AutoPostBack="true" OnTextChanged="txtSearch_TextChanged" placeholder="Type to search..." />
// Fires when user changes text and moves away protected void txtSearch_TextChanged(object sender, EventArgs e) { string query = txtSearch.Text; lblResult.Text = "Searching for: " + query; // Here you would query the database }
💡 Tip: Use 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.
💻 Complete Working Example — Login Form

.aspx file:

<asp:Label ID="lblMsg" runat="server" ForeColor="Red"/> <br/> Username: <asp:TextBox ID="txtUser" runat="server" placeholder="Enter username" MaxLength="30"/> <br/> Password: <asp:TextBox ID="txtPass" runat="server" TextMode="Password" placeholder="Enter password" MaxLength="20"/> <br/> <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click"/>

.aspx.cs file (C# Code-Behind):

protected void btnLogin_Click(object sender, EventArgs e) { string user = txtUser.Text.Trim(); string pass = txtPass.Text; if (user == "" || pass == "") { lblMsg.Text = "⚠️ Please fill all fields!"; } else if (user == "admin" && pass == "1234") { // Clear password for security after check txtPass.Text = ""; Response.Redirect("Dashboard.aspx"); } else { lblMsg.Text = "❌ Invalid username or password!"; txtPass.Text = ""; // Clear password on failure txtUser.Focus(); // Return focus to username } }
⚠️ Security Note: Never store passwords as plain text in a real application. Always use hashing algorithms (like SHA-256 or BCrypt) to secure passwords. The example above is only for learning purposes.
📝 Quick Revision — Key Points for Exam
  • asp:TextBox is 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).
  • Text property is used to read or set the value of a TextBox.
  • MaxLength limits the number of characters the user can type.
  • Rows and Columns set size of MultiLine TextBox.
  • ReadOnly="true" — user can see but not edit the text.
  • AutoPostBack="true" triggers TextChanged event when user leaves the TextBox.
  • placeholder shows 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.

Post a Comment

Thanks for comment.

Previous Post Next Post