TextBox Constructor
Collapse the table of content
Expand the table of content

TextBox Constructor

Initializes a new instance of the TextBox class.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public TextBox()

Use this constructor to create and initialize a new instance of the TextBox class.

The following table shows the initial property value for an instance of TextBox.


Initial value



The following code example demonstrates how to use the TextBox constructor to dynamically add a TextBox control to the page.

Security noteSecurity Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" > 

    <title> TextBox Constructor Example </title>
<script runat="server">

      protected void Page_Load(Object sender, EventArgs e)

         // Create UserTextBox TextBox control.
         TextBox UserTextBox = new TextBox();

         // Configure the UserTextBox TextBox control.
         UserTextBox.ID = "UserTextBox";
         UserTextBox.Columns = 50;

         // Add UserTextBox TextBox control to the Controls collection 
         // of the TextBoxControlPlaceHolder PlaceHolder control.


      protected void Submit_Click(Object sender, EventArgs e)

         // Retrieve the UserTextBox TextBox control from the TextBoxControlPlaceHolder
         // PlaceHolder control.
         TextBox TempTextBox = (TextBox)TextBoxControlPlaceHolder.FindControl("UserTextBox");

         // Display the Text property.
         Message.Text = "The TextBox control above is dynamically generated. <br /> You entered: " + 





   <form id="form1" runat="server">

      <h3> TextBox Constructor Example </h3>

      Enter some text and click the Submit button. <br /><br />

      <asp:PlaceHolder ID="TextBoxControlPlaceHolder"

      <br /><br />

      <asp:Button ID="SubmitButton"

      <br /><br />

      <asp:Label ID="Message"



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2016 Microsoft