Search Engine

Monday, May 17, 2010

ASP.Net TextBox Control OnTextChanged event

The ASP.Net TextBox control provides OnTextChanged event that enables you to get the input value entered by the user immediately after when he leaves the textbox control. The textbox control raises the TextChanged event when user jumps to another control on the web page after filling the information in the textbox control. It does not raise any event for each keystroke made by the user it occurs only when the user leaves the associated textbox control. You can handle the OnTextChanged event at server side that can be used to get the textbox value and validate it before submitting to the data store or further processing.

There are two different types of behaviors of OnTextChanged event of ASP.Net TextBox control that are based on the value of its AutoPostBack property as follow:

1. By default the AutoPostBack property value is false that does not allow the TextBox control to raise the TextChanged event immediately after the user leaves the control. It fires the event and executes the server side OnTextChanged handler code at the time the form is posted to the server.

2. The second type of behavior is executing the TextChanged event immediately when user leaves the textbox control that posts the web form to the server. This instant behavior can be set by setting the AutoPostBack property of textbox control to true.

Attaching the OnTextChanged Event to ASP.Net TextBox

You can attach the TextChanged event of textbox control in two ways as follows:

1. Double clicking the textbox control for which you want to generate the server side OnTextChanged event handler. In this tutorial we have also used the same approach to handle the TextChanged event of textbox control.

2. You can also attach the TextChanged event programmatically in the page load method of ASP.Net web page E.g.:

protected void Page_Load(object sender, EventArgs e)
TextBox1.TextChanged += new EventHandler(TextBox1_TextChanged);

void TextBox1_TextChanged(object sender, EventArgs e)


When you will write the ID of the textbox control inside the Page_Load method for example TextBox1, and will type the "." Dot operator it will display the list of properties and methods of textbox control. Select the TextChanged event from the intellisense and press "+=" and then tab key from the keyboard. It will generate the event handler server side code automatically.

Example of TextChanged Event

C# Code:

protected void TextBox1_TextChanged(object sender, EventArgs e)
Label1.Text += "TextChanged event: " + TextBox1.Text;
Label1.Text += "

protected void Button1_Click(object sender, EventArgs e)
Label1.Text += "Click event: " + TextBox1.Text;

In the above example we have used the default behavior of OnTextChanged event of ASP.Net Textbox control that executes at the time when the web form is posted to the server. When user will enter the input to the textbox control and click the button control it will first raise the TextChanged event and then Click event of button control.


Post a Comment