Export (0) Print
Expand All

Responding to Changes in a TextBox Web Server Control

Visual Studio .NET 2003

When the user leaves a TextBox Web server control after entering information, the control raises an event that you can respond to.

Note   The TextBox Web server control does not raise an event each time the user enters a keystroke, only when the user leaves the control.

To respond to changes in the TextBox control

  • Create an event handler for the TextBox control's TextChanged event.

    By default, the TextChanged event does not immediately cause the Web Forms page to be posted to the server. Instead, the event is raised in server code the next time the form is posted, as when a Button Web server control is clicked. To have the TextChanged event cause an immediate posting, set the TextBox control's AutoPostBack property to true.

    Note   The ability of a TextBox control to post to the server when it is checked requires that the browser support ECMAScript (JScript, JavaScript) and that scripting be enabled on the user's browser.

    The following example shows how you can respond to changes in the TextBox control. The code displays the contents of the control in a label.

    Security Note   User input in a Web Forms page can include potentially malicious client script. By default, the Web Forms page validates that user input does not include script or HTML elements. For more information, see Scripting Exploits and Protecting Against Script Exploits in a Web Application.
    ' Visual Basic
    Public Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
       Label1.Text = TextBox1.Text
    End Sub
    // C#
    public void TextBox1_TextChanged(object sender, EventArgs e)
       Label1.Text = TextBox1.Text;

See Also

ASP.NET Server Control Event Model | Specifying Whether Controls Post to the Server | Creating Event Handlers in Web Forms Pages | TextBox Class

© 2014 Microsoft