Presenter:Bob Tabor (from LearnVisualStudio.NET)
In this lesson, you will see how to maintain state in ASP.NET, including the various objects you can use to store information for your ASP.NET 2.0 applications.
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
In this lesson will talk about maintaining state in Web applications and what ASP net shadow does for you to manage the state across multiple requests by single user first A little bit more about the page object as well several important objects that are programmatically available to replication and for this lesson we talk about the page object and I said that it was an object that encapsulated a single request and response from a single user for a single page and that s actually a little bit of a misnomer it should work as a working definition for right now the page gave us access to important information like whether or not the current page request was the original request or whether it was the result of a post back and we were able to write code based on that the static page object contains several other static objects allows
Minute 1
Or medically warm with all aspects of a user s interaction with our web application solicitor looking at each of these objects and see what they do and how we can use them with our apps and it will be using most of these objects for the remainder of these lessons so consider this just a quick overview of the general features and not a comprehensive explanation of each and every one so first of all struck with the request and response objects to request it can really encapsulates all the information about I request from a user s web browser so whenever they tied a nation last edited of your daughter but a calm your page obvious he asked all of that request plus information about the users browser all plus any data that might be transferred along with the request in the HUD get or the record the post for similar form data or Persian data that s all contain an accessible to us
Minute 2
In the request objects now wants our application has an opportunity to manipulate the results were going to send information back out to the user we would use a response out of the recall to produce video with the page response write which allowed us to send us a quick string of information to the webpage so this response object allows us to does is it encapsulates all the information about what our application will be sending in response to the users request and we also saw the application and session objects in the previous videos and recall the session object and encapsulates all the information for single users on interaction at one period of time with our website whereas the application object has attributes and methods of the application as a whole and
Minute 3
Some stay for information and dad would say between requests Some of these more this lesson as well then you have the form object which is actually very similar to of the request object except this gives us a subset of the same information so were able to get any information that was passed along each of the post for the given request story will grab off any fields of data that were said over and just typical HUP post it as a trace object is available and is releasing the next lesson this object was information that we re going to use in order to debug and monitor our applications performance and an object called a user in this allows us to bomb a provide some security to your applications so each user can have roles is that they re associated with an base in those roles they can have access orbit not be denied
Minute 4
Access to certain resources on our website and then there s the server object and that has information about the Web server in general and keeps information about what have to file path of our of our specific application as runs and server things of that nature is an object is coming he called the previous page object and this gives us information about the previous page that was served to the user before the current page that are requests is currently handling for that code is currently handling and also I said that each of the language objects are accessible through in their parent object of page object but that s actually a little bit of a misnomer as well have because each of these are accessible without having to type in like page request or page response or page Dodge race you to type in request develops
Minute 5
Property or method or response because there is a imply a dumb import statement that s used of by our web applications okay so I m sorry to be aware of these high level objects you come use them but glossed over them up to this point and all these objects really encapsulate a great deal of functionality that we can drawn as we develop our applications but let s continue on and now talk about application state now when you use Visual Basic to create Windows forms applications in the past you didn t have to worry a lot about the state of the application because the applications that you build started up and they ran for a few minutes or maybe a few hours a time and then with the users shut down the program it was cleared out from the computer s memory during the time the program was running you may have a handful of variable
Minute 6
At store important application information like user information in and what windows have been opened or what information is loaded into the current application of those variables were kept alive the entire time that the program was alive and they were programmatically a available for you to retrieve and the changing code and also you may have had one or more forms that were visible at all times while the application was running so there was really this concert this state do it the application was as a round and was alive for long periods of time to worry about at retrieving the information every time somebody wanted to make in interaction with your application because the program was persistently in the memory of a computer until he is shut the program down but that s in sharp contrast to the way that you create Web applications each webpage is
Minute 7
In a website is has no inherent knowledge of any other webpage that same website you almost think of each individual webpage as an application in and of itself is only fired off when that page is requested so each webpage that you request from an ASP net based website is like a mini application into itself runs for less than a second what fraction of a second and based on the request rubbers the information is coming in it constructs a response and that information is then sent back to the web browser so if each page of a web application is only alive for less than a fraction of a second how he transferred the important data between webpages highly retained variable information like a setting that they used on page 1 some option of a Titan hobby you save it and to page 3 or page 4 of within your your web application or the case were performed
Minute 8
Post that hot of controls know to retain their values from post to post it never thought of that in the past had you not used ASP net developer applications these are all things that you had to worry about but once again ASP net does the dirty work the heavy lifting for you so you have to even think about most of the time however there might be some businesses were you need more control release of options about how you want to manage the state of your application so one show you a few ways it is enabled in ASP net 2 0 so first let s talk about view state on the creating the Project looks was canceled that I ve called to go to new website ergo and call this less so for tank at the okay button
Minute 9
And after a moment or two will see our source view store to the designer here in the drag and drop to test boxes need a button and the label control on orders on surface will give you some names and reference the code so honestly the links are our so called list first text box well this second test for us to call this a table of and will call into this result it will analysts go ahead and
Minute 10
Changed that and will change this survey is no set ourselves up on a double click the okay button and listen to this request on going to do results label text text box txt and we ll added an empty space character and they will put second in the text box txt going to save this mouse run or application is limited quick point here is we get started from in close locator or so typing or Bob hopes inherent in its favor in my neighborhood took okay and notice that it fills in our label control with Bob s face saver
Minute 11
Okay I understand it s easy to understand rather Howel bawled in Tabor got put into this label control that was visible before but now know is a visible but Howell didn t get the values back into these text boxes arrive us go ahead and view the source and I want to notice that there s something here called view state Sosa hidden fields of this type in the name of this field is used to include an idea view state and enhances the value of this crazy string wilts towards their crazy string of numbers and letters have is a relatively small view state with
Minute 12
This helps keep track of the values that were placed into the two text boxes of this is a mean anything you arrive but ASP net is able to look at destroying an interpreted language and translated into the values that are in first Xbox the second Xbox the label and even the okay button case so it deciphers this and it determines what the values were and so we would be able then to write an application to test and see did the text change because it was originally bobble what is a change to pistol in her of data view state allows us to do several things for salty crack of the data that was in each control from post back to post back is a very neat and we don t even know a lot about how view state works but we can use you stay ourselves
Minute 13
There are values that we need to a preserve from post back to post back within the same webpage so let s do this go back to her form comes the double click to get the page load event handler looks in this writer then who s lived as a view state add them to give this some name I can call it anything no one chooses to keep some get back to this information is a close view state item I could call the Steve but chose to call something will more pertinent to what I was working on and I m just associating with that key of value of hello world okay
Minute 14
Now let s go here tour okay button and actual abuses in a comment that line out with steel lists and on the go view state whoops keys view state item in the past and the keyname view state item and the sister to string together since I ve got so the first time our page is loaded it should have is a lot of code is saved the new value into the view state and then when we click the okay button should retrieve the value from the view state and display it in the result able to try this and see for works
Minute 15
Look at Seymour get our hello world which is what we expect to see so that s great okay to disagree Quickly view state is something that controls in your applications take advantage of to store their original values and so that its pages are posted back and back and back that the original values can be of weak dollar repopulated rehydrated from this little crazy looking alphanumeric string stored in a hidden form fields on the webpage for the client and we also demonstrated how you can put your own values within view state so that you can on a routine information on as a page is requested in re requested by a single user will assume a
Minute 16
Of what if you need to store the state or store information or values across multiple pages for a single user you can use view state in that case but there are a couple ways for you to accomplish this and one of the best ways to do this is through the use of the session object that we learned about the previous losses may recall that the session object encapsulates a series of requests from a single user how does ASP net know to keep track of each user I was in know how many users there are all during the first request to buy a new user restores a temporary cookie on the user s web browser and with each subsequent requests made by that web browser for browser sends along a special identifier and then by that identifier is the net runtime engine knows to look off all the sessions that it has currently those working with and it finds the session object of his memory
Minute 17
And its bother you though you have the information back about that user so that s pretty neat it allows you to be able to programmatically a get back to any information that was stored within the session object as the user progresses through your website okay so what s the neat thing about the session is that just like we did with view state we can matter on values to the session object close to this with a double does little to the previous lossless for a reinforced that and make this point once again I would add new item is to give global application classes global ASA asked and was due this whatever the session starts for you add
Minute 18
A new values to recall that some value and was going to put in that value demonstrating sessions a day and then in our default aspx was this or actually write here what it was common that out over this again and this time will go to and perhaps we should do to string as well that titles from this application and click the okay button so you see here that we have our little strength and
Minute 19
It was stored in our session as the session was created in the global event handlers session_start such a replace this value and then we retrieved it whenever we click the OK button and handled the OK button click event was to something else is a little more for see and use up for your so instead to some value want to do is go session dots of CD session ID okay so this is going to give us a unique identifier that is normally saved within the users web browsers cookie tag or they will does if they ll be fine and will do so and see what one of these identifiers look like
Minute 20
Okay once again it s a crazy mixture of letters and numbers of this uniquely identifies my browser session on this web application at this point time if I were to hit refresh to continue to see the same values if I were shut this down and wait for about 30 minutes and then hit the refresh button I might see a different value care because a new session was probably created for me in memory sticks because if you remember from the previous lesson sessions are discarded after a certain period of time when the other Web server assumes that the user has stopped of ill visiting pages on this particular website so the session with a die okay continue on
Minute 21
And posted back to itself warm or times but the session works across multiple pages for single user in the view state is session object work is finals the time they do have some inherent weaknesses for small neither are strongly typed so we re going back here we in the default aspx WB and take a look at the code where we access the value session some value noticed that the key that gives us access to the value is a string literal safe thing is true for the view state noticed that we have view state item is a string literal to key to grab the value of the webpage to store their okay so what happens if we were dispelled this some value as were setting a value into the session object but we spell it differently accidentally whenever we are accessing or getting the value back out of the session while the compilers not defined at what could happen is were going to be running our application testing it or
Minute 22
Worst one of our users are using replication and are not getting the results they expect because was being returned is an empty object okay so this would be difficult for us to track down what would be better is if we could just go session some value this would not only give us IntelliSense help is validation that were using the correct property but they would also gain the value of the of the code editor and compiler telling us if we spell something incorrectly so would be nice if this were strongly typed secondly it would be nice if we could have some flexibility and where the values are stored currently as we ve noted the session object store stars in memory which could scale pretty badly if we get like a sudden rush of users tour website and pay medicine approved today is the unedited auto however it still can be somewhat of a concern also if we were to try to store a lot of values within our view state that makes a
Minute 23
Pigs really big which increases the load time of our webpage and the transfer time across the Internet so is there a better way will in ASP net 2 0 we have is called the profile object now it has two big benefits first of all it s strongly typed which means that we get the full benefit of any other variable or any of a property of an object within our system and incessantly we get the flexibility of where we store the values you can configure this to be stored for the values we stored in the database us in server database access database and XML file in memory where ever we want to put it also is easy to set up and use it really works best for authenticated users but also more for authenticated users of our website on them explode that means later once we introduce membership in the login controls in the functionality of that at those contained within a couple lessons from now
Minute 24
Or rather the anonymous features of the profile object to show how to do that right now will do is define the properties of our profile object by going to our web config file really haven t talked extensively about web config yet really talk about that in the next that you will do more from the edges double click this file and go to the very bottom and when peace is encoded hopefully this ll make sense there is an XML file called web config to contain the settings for website it wanted to place a web config file whenever we went to go debug our application we said yes so that s how the file hearing to our website in the first place now only do is paste some XML into my web config file and first longer talk to this in a very high level answer to so you can come to get a basic understanding since we re not using any
Minute 25
Of membership or login features of our website really set the anonymous identification able to go through it so virtuous but what that means rainouts except if they sell you that were going to use that for our particular example were going to enable the profile object and then begins at properties to that using an ad elements setting the properties of the ad element name equal minute property and then allow anonymous equal true as we can also set the data type so if we didn t want to be a string which is the default is set to an integer or a double or whatever the case might be in stores adding one property to this thing called a profile object that s all we re doing in config to say that I m going to close that file and a comeback oversteer and comment this out and begin then to work with a new profile objects of your
Minute 26
Going to profile dots and notice whenever I get the IntelliSense nose at my new property comes up it shows up under the profile object out of this work will the profile object looks to the web config in any properties that we defined therein and it allows us to use those within our application service at my new property equal to some value in the so called red paint the house I ask is that property exact same way that I do it whatever I m writing normal about code access a property of an object right song to do this results label attacks equals profile my new property is all there is to it in this restored across multiple pages of our sites
Minute 27
Or multiple posts to ask for the exact same page so I ve run the application to test it in nominal click the okay button in order to retrieve the value that was said of the profile during the page load and notice that horrible pull that value back out value bread which we place our label or equal so this is probably now the preferred way to save state within ASP net two auto applications and we have shown some of the more complex features were able to define where those dollars are stored when a database sex in the fall or a case might be a more dance but for right now is except the fact that you need is a state user profile object in the researcher will be more your own opinions more the extent of functionality so now let s continue lawn okay but what he needed me
Minute 28
In state media some sort of global setting across all users and all pages of your website was a solid previous lesson the application object can fit the bill for this purpose the recall the previous lesson how we created a poor man s visitor counter how we incremented every time a session started week incremented a special value of called on user counter and whatever the session ended we we decremented data and displayed it in with a stomach I take the time to redo that example is refer back to the previous example but that was something that work across all the users all of our application across all the pages that were in our website Bell had access and could make modifications to that application on a very bold that value that we store the application object
Minute 29
Site one of the most common ways that you might be familiar with majority a familiar with web development is to use a query string song uses hyperlink control and the beauty of a hyperlink control is that it allows us to programmatically change URL and the text that the given hyperlink control points to so we could for example point hours over to hardcode in this case we could do this in the code behind and do dynamically or bind it to a database or the case might be were going to data binding to another page in application will have another page was going to add what is for quick so let s all select add new id recalls this page to table his click add to an anonymous page of said liberals label control of the coldest
Minute 30
Do little to analyst is get rid of all the texts are up and so was my page 2 and this is going to be the target from our hyperlink or own a Jewish hyperlink to navigate URL property is a watertight bin page 2 aspx them to use the To pass in the value and really call this value the name of the value of the see my value something rather unoriginal and then I ll be actual value of the values of the name value pair is ball got Sosa passed the word ball over to page 2 and page 2 can reference that value through aim my value will tag name is over here at page load will will do
Minute 31
Is going to own requests forms my value it was at that status label text do nothing to do with single your just hot result label and right so let s put on rundown and let s start with the page you re been started up a click the hyperlink
Minute 32
Okay so let s not talk about some other ways we can pass data between one or more pages on a website we can use a more traditional approach of using for sample query string in a query string is just a series of information is appended to the end of the webpages you re all that is used to transfer information so you have a question mark and the name value pairs he might have on my value name equals Steve or Jim or Bob or whatever the case might be a so let s use that technique in the way Ramona do this is through the use of a hyperlink controls was to drag drop a hyperlink on for design surface now the beauty of using a hyperlink control is that we now have programmatic access to what the text property says of the hyperlink in focus we can change that dynamically and also the navigate URL property which allows to change a
Minute 33
Typically wears this hyperlink points to get me to do that no such as a younger target list and add another webpage called H2 and not added to the page for page 3 water and there s a new item page 3 use this page 3 digital on a page to what I want to do is score returned on a drag and drop a label controls to call this result labeled bouquet and sister were the text is in a little and I really go battle actually own a double click summer the white area on page 2 aspx so that I get my page load the most and have a code behind a forgot to check a little checkbox whatever was adding new items but that s okay we can make this work for right now so well if you call this results label text request
Minute 34
To recall the request object and pass a mystery which is the key solicits some name value pair them expecting to retrieve from the request object so she will personally like my value in here to string loops to string there without a pet s and now what we need to do on our default page it is comes the hard coach is this property and our property window Rigo page 2 aspx in the user Story to start a query string call my value which is the name of the name value pair in cold to Bob which is the value of the name value pair that it s in now all was one broader application and rare click on a hyperlink and noticed that
Minute 35
In my URL I have page 2 aspx The query string starts here and in my name value pair the names call my value the value equals Bob and I retrieve data from the request object and displayed it in my label control to ask her to that work just five so that s one way to transfer information between two pages to set it within a query string in the retrieve the value of the query string using our request object possessed the second way that we can transfer information from one form to the other analyst for the page to now actually let s go to page 3 so we have our little party label call this result labeled in the change to name the text property blanket out a
Minute 36
And it s what I would do here in the page load is put some results label text request form convert expects some value to be posted to this page and was called his post back text box could talk to string looks to use terrain there we go and it s not a page to what we wanted to in our design view to add a text box and a button to the mother take the time to rename these will use them as it is for them to click Dion I m going to actually select the button now well I do here is a
Minute 37
Is a little bit of a new technique in ASP net 2 0 and as it gives us the opportunity to post back to a different webpage so typically when I click the button was to happen is going to load up page to once again as opposed to whatever I type here in this text box that takes too but I don t want that to happen I want this text box to be posted to another page using the regular old HCB Co of post that available even was available 1015 years ago in an AGP so why would do is set a property of this button if you scroll Find the post fact URL and here I can click a little button next to this property and I can select another page of my website I m going to post the form data to live the user clicks the okay button to do so and now
Minute 38
Oh all I need to do is actually asked to want to change the name of this to whatever I call this year so we called were going to look for this name this strain called post back text box that s what I have been name my text box in order for your treat correctly or change this from pitstops one to post back text is yet pasted here in the IDE soon everything should work whatever I click this button anything I type in here should be delivered to my page 3 which will then grab it from the request and put it in the result labeled on my page slows to a head and run this application and careless the slight code out stop this
Minute 39
Comment that out and go for his or her previous example of it our business okay so now we re in business in a click the button and it posted whatever I typed in to that text box to page 3 so this is a cross page posting is what this is referred to as it allows us to exchange data between webpages in a more traditional way on that we may been familiar with if you have used another a technology to build dynamic webpages in the past okay so this lesson we talked about the page object and some of the objects that belong to the page that encapsulate very as aspects of a user s experience on our website and with those objects we can programmatically manage the state of our web application we talked about state
Minute 40
The state of the server including view state in session objects and that we also have a mother was a passing data between Web request such as the query strings and cross page post asked other socialism of the ways to maintain state that we really didn t talk about like managing cookies on the client for example in ASP net 2 0 provides a simple programming interface to manage that blood run a cover that in this particular lesson but if you enjoy this video please visit WW don t learn Visual Studio net to download and watch or former videos just like this one in all skill levels on many different topics related to see sharp Visual Basic ASP net and a lot more thinking
Thursday, May 29, 2008
Video: Understanding Web Application State VB
Labels:
web application state
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment