Introduction
A textbox is a type of webform control that takes user input. There can be cases where a user must enter an input before the server can generate an output. A textbox is an empty area that can hold user information. This information can then be manipulated and used to return meaningful information.
We often encounter textboxes on a website. Mostly while filling a form. A form is a typical example of a textbox as it requires the user to enter their information in small boxes.
In this blog, we will witness how a textbox is created, the types of textboxes available, and how they can be used to generate results.
ASP.NET WebForms Textbox
To create a TextBox, we can either write code or use the drag and drop feature in Visual Studio.
To add a textbox using the drag and drop feature, we first need to go to the view tab and select the toolbox option.
With the toolbox now available, we need to drag and drop the TextBox into our web form file.
Upon dragging the TextBox, the code for the TextBox is automatically entered by Visual Studio, and a TextBox is visible.
The server-side asp has its own code to create a TextBox.This code is in the form of a tag which looks like this,
< asp:TextBoxID="TextBox" runat="server" ></asp:TextBox>
When the server renders this code it is treated as an HTML control and the following code is provided to the browser.
<input name="TextBox" id="TextBox" type="text">
This control or tag has the following properties-
Property Name | Property Description |
AccessKey |
Used to set a keyboard shortcut for the control. |
TabIndex |
Has the tab order of the control. |
BackColor |
Used to set the background color of the control. |
BorderColor |
Used to set the border color of the control. |
BorderWidth |
It is used to set the width of the border of the control. |
Font |
Used to set the font for the control text. |
ForeColor |
Sets the color of the control text. |
Text |
It is used to set the text shown for the control. |
ToolTip |
Displays the text only when the mouse is over the control. |
Visible |
It is used to set the visibility of control on the form. |
Height |
Sets the height of the control. |
Width |
Used to set the width of the control. |
MaxLength |
Used to set a maximum number of characters that can be entered in the text field. |
Readonly |
Used to make the control read-only. |
An example syntax for the asp tag is as follows,
<asp:TextBox
AccessKey="string"
BackColor="color name|#ffffff"
BorderColor="color name|#fafsfs"
BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
Inset|Outset"
BorderWidth="size"
Columns="integer"
EnableViewState="True|False"
Font-Bold="True|False"
Font-Italic="True|False"
Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
Large|X-Large|XX-Large"
Font-Strikeout="True|False"
Font-Underline="True|False"
Height="size"
ID="string"
MaxLength="integer"
ReadOnly="True|False"
Rows="integer"
Style="string"
TabIndex="integer"
Text="string"
TextMode="SingleLine|MultiLine|Password"
ToolTip="string"
Visible="True|False"
Width="size"
runat="server"
/>