Home
Username:
Password:
Microsoft ASP.NET 3.5 Tutorials

Next Level ASP.NET / Events

Subtitles of the Movie

Now in this video I want to talk about Events because Events are an integral part of what we're trying to do here. What we're really wanting to do with ASP.NET is build a Web site where the web pages react or respond to what the user does. They click on buttons, they click drop-down lists, they click on hypertext links, they do various things and any time the user does something we want some code to run, or we want some functionality to happen. Now this can be server or client side, we'll get into that later. But for now I'm going to add a second button on here and I'm just going to drag it on and put it right there, I'm just going to grab it and pull it up under that one and I'm going to go change the properties on this and I'm going to change the text to this button to say Button2 and when I click you'll notice it now says Button2. Well, if I go to my Code Behind page by clicking on the top tab up here, you'll notice there is no Event back here for clicking on Button2. So what I can do is a number of ways I can put it there. First of all I can go right here and just type it in and I'll have to type it based on this syntax up here on this button Click Event and that will take me, for the way I type and talk, couple three weeks to get that in there and get it working right, so I don't want to type it manually. So what I can do is I can just simply double-click on the button and it will automatically set it up and now I can start to write code, right and I can put as many lines of code as I want and every time the user clicks on that Button2 this code would run and when it gets to the Server this code executes and it does whatever I want to. It connects to a database, it pulls data out, it writes data in, whatever I want to do and so that's one way to put it there. Now I'm going to take that out and show you another way to put it there. If I click on this button and go back to my Properties page and I'll pin this up, some of you guys who came out of the VBA, the Visual Basic for Applications environment, or the Access environment, maybe you're used to using this Events button here and so what I can do is, since I'm on the Button2, if I just double-click right here it will set up the Event Handler for me, but there is something else really cool that I can do that I want to show you and I don't want to confuse you here, but stay with me here. I can type right here Mark and since I'm on Button2 now if I double-click right here notice it's created the Button2. I'll just take that one out, but it now has a button sub called Mark and if I scroll over you'll notice that it handles Button2 click, so I've got a subroutine named Mark but it handles the Button2 click. Now, here's where I'm really going to confuse you - I hope not. But I want to point out something here in the .NET Framework. What really works to make this handle that particular Event is this Handle section right here because what I can do, notice, I'm going to take this out and I'm going to come right here and I'm going to change this. I will delete this and I'm going to say Handles Button2.Click. Now notice it says Button1 Click, but it's, when I click Button2 is what's going to be handled here and it'll say You clicked. So notice that. I'm going to click Button1, nothing will happen. When I click Button2 I'll get the result. So let's run this, we'll run it without debugging, my page will come up and I'll click Button1, notice it came back, the Label's still the Label, nothing happened. If I click Button2, it comes back and it says You clicked. Now what's the big deal? The big deal is I can redirect my Events by the use of this Handles keyword here and so if I take this out my Intellisense drops down and says, what do you want to handle here? Notice, on this I could even handle my drop-down list anytime something has changed on that, the text is changed, or the selected index changed. I can handle it all off of this sub, so what I'm going to do is change this back to Button1.Click and I can even put a comma here and say Button2.Click and now just by pressing Button1 this same button will handle Button1 and Button2 clicks. What I'm trying to show you here is that I can have this code run when somebody clicks Button1, if I put comma Button2.Click when they click Button2, this code is what runs. You have a lot of leeway with your events now in the .NET Framework. I hope I haven't confused you with this but I want you to go back and look at this, but you can use this to create your own subroutines as handlers very easily, to name them what you want, to have one handler handle a bunch of things - there's all kinds of possibilities now. That is a function of the .NET Framework in ASP.NET. That's Events. Now, I want you to think Events and you're going to see as we go through here we're looking for Events that when a user does something they click on something, we're going to send this page back to the Web server, the Code Behind stuff back here on our Code Behind page is going to be executed on the Web server, the result of that is going to be compiled and sent back to us in HTML form and shown to us in our Web Browser. That's a good basic overview of Events in ASP.NET.

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