More About Events


So far, our application looks just like a desktop application; the form displayed our initial drag-and-drop and property settings, and it responded to a button click by returning to the processing computer for the logic. But let's be honest. There's no way that a web application can ever be truly as responsive to events as a desktop application. What happens when the Internet connection goes down or it is just plain slow? How do you handle things like TextChanged events in text fields? You can't have the web page go back to the web server every time the user types a key.

The ASP.NET TextBox control has a TextChanged event, but it does not trigger for each keystroke. In fact, it doesn't trigger at all (by default) until something (like a button click) causes the page to go back to the server. And there are a lot of other control events that work like this. They are all saved up until the user does something to bring the whole page back to the web server for processing. At that time, these delayed events finally fire, and processing continues as normal.

So there are really two types of events: regular and premium. I mean postback and non-postback. Postback events are those that cause the web page to immediately return to the server for processing. Non-postback events delay their event handlers until something else causes a return to the server. Most events are one or the other, but some can be changed. The CheckBox control has a CheckedChanged that fires (in a non-postback way) when the user alters the state of the checkbox. However, if you set the control's AutoPostBack property to True, the page will immediately return to the server anytime the user clicks the checkbox.

Besides control events, the entire page has a few events. The most significant is the Page_Load event. This is analogous to the Windows Forms's Form_Load event; it's a great place to configure initial control properties, fill in drop-down lists, etc. I'm going to add the following code to my page's Load event.

Protected Sub Page_Load(ByVal sender As Object, _       ByVal e As System.EventArgs) Handles Me.Load    ' ----- Initialize the data.    Product.Text = "No Data" End Sub 


Now the Product label will display "No Data" the first time the page appears. The thing about the Load event handler is that it runs every single time the web page is displayed. Because this test application keeps using the same page over and over again for its results, the Load event handler will run afresh each time. For this test program, it's not really a big deal; the code in the ActMultiply_Click event handler overrides its initial "No Data" value. But in other applications, you might not want to keep reinitializing the data. Fortunately, the Load event will let you know if this is the first time through or not through a page-level member called IsPostBack.

' ----- Initialize the data, but only the first time. If (Me.IsPostBack = False) Then Product.Text = "No Data" 





Start-to-Finish Visual Basic 2005. Learn Visual Basic 2005 as You Design and Develop a Complete Application
Start-to-Finish Visual Basic 2005: Learn Visual Basic 2005 as You Design and Develop a Complete Application
ISBN: 0321398009
EAN: 2147483647
Year: 2006
Pages: 247
Authors: Tim Patrick

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net