Home Page

Click here to download the sample source code for this lesson

Some of the server controls in ASP.NET have an event called Text Changed.  This event is fired by ASP.NET anytime a user changed the text contents in a control and press the TAB key or put the focus on another control.  This event allows you as the programmer to dynamically know if the user typed something and then your code can perform some actions based on that event.

Create a new web form named Default4.aspx and go to the Design view.  Drag two TextBox controls onto the web form and name the IDs txtSource and txtDestination accordingly.  Double click on the txtSource TextBox control and Visual Web Developer will create an event handler named txtSource_TextChanged.  Again notice the naming convention, ID appended with event type.  In the Method, type the following:

txtDestination.Text = txtSource.Text;

Go back to the Design view for the Default4.aspx page and we need to set a special property in order for this example to work.  Click on the txtSource TextBox control and change the AutoPostBack property to True.  This ensure that if something changes in that TextBox and the user presses TAB, the web browser will automatically post back the form or call the server to request the web page again.  If you don’t set that property, the web browser will not request anything from the web server and your txtSource_TextChanged event won’t fire.  To summarize the sequence of events that occur when the web page runs in our example:

First, when the page loads, ASP.NET will render two empty TextBox controls.  When the user types something into the first TextBox control and TABs off the field, the browser will request the web page from the server again (this is called a PostBack because the web form is “posting back” to the server).  When the server receives the request, it senses that the TextBox control’s contents have changed so it fires the txtSource_TextChanged event Method.  ASP.NET is handling all of this interaction between your web browser and the server behind the scenes.  This abstraction makes it very productive for web programmers because they can focus on programming their business logic and let ASP.NET handle all the HTML, Javascript and other technologies that the web application needs to work.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment