ASP.NET - Textbox Control

A TextBox control in ASP.NET is a graphical user interface (GUI) element that allows the user to enter and edit text. It's commonly used in forms for capturing user input, such as login pages or search bars.

Events:

  • TextChanged: This event is raised whenever the content of the textbox changes. It's commonly used to trigger some action when the user types or deletes text in the textbox.

Methods:

  • Clear: This method clears the content of the textbox.
  • Focus: This method sets focus to the textbox, making it the active control on the page.
  • Select: This method selects all the text in the textbox.

Properties:

  • Text: This property gets or sets the content of the textbox.
  • ReadOnly: This property determines whether the user can edit the content of the textbox or not.
  • Enabled: This property determines whether the textbox is enabled or disabled. When it's disabled, the user can't interact with it.

Example

<asp:Label ID="lblName" runat="server" Text="Enter your name:"></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

In this example, we've created a Label control with an ID of "lblName" that displays the text "Enter your name:". We've also created a TextBox control with an ID of "txtName" that the user can use to enter their name.

Finally, we've created a Button control with an ID of "btnSubmit" that the user can click to submit their name. We've specified an event handler for the "Click" event, called "btnSubmit_Click". This event handler will be executed whenever the user clicks the button.

Here's an example of how you might handle the "Click" event in the code-behind file:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    string name = txtName.Text;
    lblName.Text = "Hello, " + name + "!";
}

In this event handler, we've retrieved the content of the TextBox control using its "Text" property and stored it in a variable called "name". We've then used that variable to set the text of the Label control to "Hello, [name]!", where [name] is the name entered by the user.