How to: Respond to Changes in a TextBox Web Server Control
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. You can have the TextBox control raise client-side events that you handle in client script, which can be useful for responding to individual keystrokes. For details, see Client Script in ASP.NET Web Pages.
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. 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 code 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 How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings.
Protected Sub TextBox1_TextChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles TextBox1.TextChanged Label1.Text = Server.HtmlEncode(TextBox1.Text) End Sub
protected void TextBox1_TextChanged(object sender, EventArgs e) { Label1.Text = Server.HtmlEncode(TextBox1.Text); }