Google Search

Google
 

Thursday, May 29, 2008

Video: Understanding More About Events and Postback VB

Presenter:Bob Tabor (from LearnVisualStudio.NET)

In this lesson, you will learn how events can be used in your code, and how to react when those events occur


Click here to go to original video page

The following text is a software generated transcript of the video. Click on a minute link to jump to a location within the video

Minute 0

Collodion in this lesson were discussing how to handle events and set properties and code on the server and talk again about the concert post back and programmatically determine whether or not the current web request was a result of a post back or of first time request an organism about why that even matters and finally really talk about the events that that are in a more global level in nature at the session and application levels over this makes sense then you re in the right place I ll try to explain all this pretty thoroughly OKs west to get started let s review some of the things we said earlier about how events are handled in your application all the Visual Basic code that you write gets compiled into a net assembly the individual aspx pages contain a page directive at the very top which points to a specific class in the net assembly that s created
Minute 1

Solar page request is made the ASP net engine sends along information about whether this is the first time a request for this page or whether it s the result of a post back as well as which event actually cause the post back like a button click for example it delivers the Web request to the events handler which is where you ll write the logic for your site so the first lesson we talked briefly about how the pages been constructed and then passed back to the web browser that originally requested it to the three things that can affect the output the first thing is the HTML is contained in the aspx page secondly the server control tags in the aspx page and thoroughly any modifications are made to the server controls as events are handled within the net assembly for the website so there still with me can forget what I just said take comfort in the fact that he just works but know that
Minute 2

In handles the heavy lifting which allows you to focus on the business functionality in the logic of your applications so why do I bring all this up about post back in the first swelters two reasons first of all post back is very cool because it helps create the illusion that the user is working on a robust full featured application as opposed to working with just another website in other words it enables a more interactive experience that is responsive not just to button click event similar to submitting a form but also to respond on the server for small events on the webpage like the selection of an item from a drop down list box luck was on the previous lesson or clicking on a checked checkbox at that point we can interact with the user and change what the user sees in the spring give him information or give him a different webpage so if we were to try to do that without ASP
Minute 3

That it would require a lot of custom JavaScript coding ab but fortunately it is there that does all this for you when you have to write any code make it happen to set the set a few properties of a slow start with the easy part we ve already done this in the previous two lessons but we just accepted it at face value and that is the setting of properties of our ASP net server controls in code so he knew something like this button to get to the button one click event handler and then do some like this text box to text stocks forum textile whatever s been typed in a text box 1 will do then be placed in the text box to something very simple and straightforward teleported out the first video noticed the IntelliSense that shows us the properties whenever use the dot notation pops open all of the most common properties that can be adjusted with our applications within on the second tabby
Minute 4

Notice that not only do I see more properties by also see all the methods that are available such as the two string method and lets see some of the things that might be important to us to check to see if it has any sub controls on rid to determine and set weather has focused or not and so on and so close the been most cases where does one work with the common properties that are available here which is an abbreviated list of most popular ones that are available so realistically while there s just a few properties of a text box or any control for that matter that I might ever want to change the fact is that I can set or get every property of a given control within my event handler so that a lot of power available to us programmatically as the greater applications on this case were to remove this and let s just
Minute 5

Iran or application approved at this point that it works asked me if I would thought okay and add the web config room talk about that in a listener presumed okay so I typed in the Boston click the button to get Bob in text box to write so worked perfectly so was the launch and talk about the next step in this process and just like there s a form load event available whenever you re creating Windows forms applications in Visual Basic 2005 are for a previous version of Visual Basic we also have a page load event in ASP net that fires each time a page is requested from a web browser source for double click anywhere in the white area chair and notice that it gives me a page_load event handler so
Minute 6

Err initialization purposes or to set default values and so forth but the trick is is that this event fires every single time the page is loaded even if the page is posted back once twice three times or more so this is where it is tricky let s add this code with the intent of initializing the text box to some default value soldiers if Xbox one txt equals empty string so this looks innocuous enough right I mean all are trying to do here is that our text box 1 when the page loads to an empty string to the default value out which we may do in other applications novelistic lot about it all seems to work for a very known element filled were bought a click the button again
Minute 7

But now what happens all I did was just a text box 1 equal to nothing vote what happened here was broke will actually nothing broke it work exactly the way that we told to work it s really a matter of the order in which these events are firing page load is firing before the button one click event so in the very first page load here were doing is setting the text property of the text box 1 evil to the empty string and then the next event that fires is about one click event works as Xbox two txt equals ex boss want which is now currently an empty string so how can we overcome this problem will get news is that we can check for this condition by checking whether or not the page is being loaded for the first time or whether it s the
Minute 8

Loaded as a result of a post back and the way that we do this is the property of a special object called a page object which is the is post that property in a page object contains a bunch of useful properties and methods and other objects pertaining to the current request in the current response for the current page so only at a little bit code here so if page it is post back equals falls within okay so what are we doing here or checking to see if this page is being posted back then and only then if it s not being close to back will we see execute this line of code if we are in the post back situation and we will ignore this line of code that so the first time are form is loaded with the
Minute 9

Line of code that fires that says this to an empty string works correctly the second time where we click this button now is post back equals true little ignore that line of code in our application functions correctly once again I hope that made sense or to move on before it closes less and less hobbled the lifetime of our web applications for the first time a request from a web browser is made for any page within our website project the object called application is created and maintained by the ASP net runtime engine so this application object is cut especially contains information as global to the entire website regardless of how many people access it or how many pages are contained within it it s kind of the Millwall be all object that contains information about our instance of our application running on
Minute 10

In the Web server so when the application is been inactive for a long period of time or when the website has been stopped at the Web server level the application is object is removed from the computer s memory a similarly there s another type of object that s created every time a user accesses a website in its called the session object soul session can span multiple requests but only for a single website user so give 10 people accessing your website at the same time there will be 10 session objects in the computer s memory one for each user regardless of how me pages each user has looked at whether one or hotter pages so after a long period of inactivity like let s say 20 minutes or so the session object is destroyed from the Web server computer memory and the reason why destroys is that it can t simply keep around all these are
Minute 11

Every user also use up all the memory within the computer so where you revisit the application and session objects several times drop a series of lessons horses were talking about events right now let s talk about how to handle for events that are triggered when the application starts and when it ends in women who session starts and when it ends so in order to handle beast for events and we just talked about were given me to add a special filed for a web project called the global ASA acts in the way they really had that is to select the add new item button on our main toolbar and spiritual bar and then were going to select the global application class notice that it by default names of global ASA acts of rapid change it or click the add button okay so now it has five handlers in place
Minute 12

It has a handler for the application starts in the application isn t it also has a handler for whatever a session starts in whatever session ends so we can write code every time or application starting to initialize and cash were taught while caching will later we can store up some of the common values that we might use for our application or can store information about individual users on our website or every time for example a new user comes a website and keep track of how many users are currently on the website and let me show you how that s done was to list was rising cochair of the application object has a neat little feature to that allows us to add coming variables at this global levels will recall this user account was to add a new very will miss how we do it
Minute 13

Object we don t have the given type is of type object which means as a generic object and select the cast and whatever we re ready to work with it but what is said the whatever the application starts to initialize this user count variable called user count reset to zero in each every time that a new session starts will do this application looks application user accounts and they will use the plus equal one to increment the user count by one and then we ll do something similar whenever a session ends so there are no user counts minus equal ones so will decrement that user count application variable in this and I want to do is actually show on screen
Minute 14

What the current user count is so slow to our default aspx VB file and from here were going to will solicit us was put outside of this raise our page object to get a member we said of the page object controls information about the current user s current request and the current response from our from our website back to the user for the current zone for the current page of her user up there so the page has a request objects are sorry our response object store able to ride out to our response stream that information is to be sent back to the user so page response write user accounts
Minute 15

And they will do this application and user accounts 2 string octet s notice that we have access to this application variable from within any worth our application that s because it is a very global level we don t have to opt for a preface it by any other bill durables were or were objects we can reference this at any point in time granddaughter application now and noticed that the user count is set to one ballistic missiles copy this URL open up a second web browser to simulate a second user on the paste a web address and noticed that the user count hits to go forward a refresh this first browser noticed that the telcos
Minute 16

Two okay no matter how they times are refreshed each individual browser to user count is still too little to the third browser will for the similar results here David comes three okay now forward way around here for 20 minutes it would decrement this count as well as each session object dies and is removed from the computer s memory okay but in this lesson we talked about how post networks and how the ASP net runtime engine knows to pass along the notification to the event handler that should execute based on the post back of a control as far by the end user s web browser we talked of a special event called the page_load event and we discuss how to programmatically check to see whether the page request was due to an original request or oppose back in final talks with the concepts of application and session
Minute 17

And how to write code for the major events in the life of an application and session and I hope you enjoy this video if you do please visit www learnVisualStudio net to download a watch or former videos just like this one and all skill levels on many different topics related to see shark Visual Basic ASP net and more thank you

No comments: