Home
Username:
Password:
Microsoft ASP.NET 3.5 Tutorials

Next Level ASP.NET / Page & Control Events

Subtitles of the Movie

Now in this video I want to talk about Page Events because when we start talking about writing all these events into our Code Behind pages we need to know about some events that are out there that we can code to that we might not be aware of. Now first of all, let me give you a little diagram here about what I'm talking about with these Page Events. When a user is out there and they open their browser and they connect to a Web site and they request a page they hit a Web server and there is a page on that Web server that they want to see. They're going to type www.mark.com/prices. aspx. Here's the prices web page. It's sitting on the Web server. Well, when they request that that page gets processed by asp. Now what happens is, is the Web server grabs that page, pulls it up and says let's send this back to the browser and then it sees, hey wait a minute, there's some ASP.NET that needs to be processed here and so it sends it down to the ASP department on the Web server. What happens in that process, as that page gets loaded by ASP.NET it goes through various stages and these are called Page Events and then the Controls themselves go through these stages and we can actually write code and so as it goes through those Events things happen to it and get added to the page or changed on the page, then finally the page leaves ASP.NET, it gets rendered as HTML and goes back to the browser. Well, let's take a look and let's talk about some of these Events. There's a number of Events that fire and they always fire in a pre-determined order. They're automatic. And you can write code to make things happen. There are more Events than what I'm going to show you here, but I just want you to be aware of some of the major ones. First of all there's an Event that's called PreInit, or Preinitialize, but what happens during this is as this page gets loaded into ASP.NET on the Web server the PreInit Event is fired and this is fired when all controls that were created at design time get initialized with their default values, so let's say I put a text box on a Web page and I tell them that I want the value of the text box to be the city that I live in or the city where the Web server is. Well, at that point, that Web server sees that and assigns that default value, that text box, to that control. We can also assign values to our page as well. Now on all these Events, most of them, they will first apply to the page and then they will go through the controls as well. On initialization is separate, it happens after PreInit, at this point read the control properties and do things with them in code, then there's LoadViewState. LoadViewState fires if the page has been sent to the Server already and is returning again, or in other words, if the user has requested this page, a request was sent to the server, we sent the page back and now the user has clicked on a button and they're sending the page back for processing. It is being posted back to the server. We'll talk about this later. I'll show you a diagram on it. In the LoadViewState, if it's being returned to the Server again, if it's being posted back, then any control property that had ViewState enabled we will see those properties and we'll talk about ViewState a little bit later as well. Next up is the LoadPostBackData. Now again, if the page is being posted back to the Web server we can load data from our controls or to our page from the HTTP POST data. If you know what that is then I just turned a light on for you, if you don't, don't worry about it. PageLoad is the one that most beginning programmers use, it's most commonly used. What this simply means is whenever this page loads into memory in the ASP.NET department on the Web Server, this Event fires and what it really does is it checks this Event to see if we've written any code that we want to have happen. Now most people will use, most developers, will use PageLoad to set any properties, to set any values, to check for certain conditions on the page. Again, great place to do it because this happens every time the page loads up. Then there are some others that I'm not going to mention here to keep from confusing you any more than I already have. The last one's called PageUnload. Just before the page leaves ASP.NET and gets sent to the browser the PageUnload Event fires. Let me show you how you can see these things. I'm going to open the Visual Web Developer here and I'm going to show you that if I just double-click on the page somewhere and notice the PageLoad Event shows up and if you'll notice up here in the left drop-down box I can see my Page Events and on the right side I can see all the Page Events. Notice there are some I didn't show you. Here's Init, PreInit, PreLoad, here's Load, here's Unload. It would really help you to go out and read up a little bit about these various Page Events and what you can do with them. Anything that I want to have on this page, if I want to set a value of a certain control, let's say I want the Label1.Text to be Mark then this is going to set that. When this page loads in memory the text is going to be Mark and it's going to sit there as Mark and it's going to change to You clicked once I click the button, but what this is going to do is, it's going to set that Label value, notice, to Mark. Soon as this page loaded on the ASP.NET Web server it loaded it to Mark and then it sent it out to the user as Mark. Make sure you understand your Page Events and also, let me show you before I leave, every one of your controls, the Button1 Control that I dropped, it has Events. Notice it has a Load, an Unload, a Click Event and so forth. So just make sure you understand what these Events are and how you can use the built in Events to do a lot of cool stuff to your web pages and your controls as you develop your web pages.

Tutorial Information

Course: Microsoft ASP.NET 3.5
Author: Mark Long
SKU: 34102
ISBN: 1-93633412-7
Release Date: 2010-03-24
Duration: 6 hrs / 69 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available