In this video Chris Pels will show how to persist and restore information in the ViewState of a custom user control. First, learn how to persist simple values containing .NET primitive types such as String, Int32, Boolean, etc. Then see how to persist complex types such as Class which contain properties storing information. The sequence of processing View State information during the lifecycle of an ASP.NET page is also discussed.
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
Of role is just as this digital original decision reserved you see the custom user control let s get started by creating a website and this website in old code that comes with it is available in the download that comes with this video seeking get it downloaded and then look at it in more detail to want to do here in our website is create a custom user control him and called his number info no member info is a user control that we re going to use to store or I should say display some user information on every page of our website end of this website are retreating is a shopping based website and when the user first visits the site logs in the site will retrieve the
Minute 1
Member information from the database and display it in our member info user control in and on subsequent pages or post backs of the same page rather than go back to the database and retrieve the member info every time or wouldn t do is write some code that takes that member information and process the review state of our member info control some ago into the design mode for our user control mode want to do is display several pieces of information about our member in this user control so what I m going to do is add several labels to the design surface of our youth control than the copy and paste this first label so we get several here on the youth control design surface now let s open up the code behind for our user control in this support will do our work in terms of interacting with the view state associated with the user control suppressing an
Minute 2
Do it in our code behind class is at a private method called load member info blog and do here in November and so is the first time through this method were going to get the member information from the database and for the sake of this example code were not related to go to a database which is a hard coding assignment of some values to the labels and then will persist back into view state and will we wanted to hear and member info or load member info is test to see whether or not the member information is in a state if it s not that we get it from the database it is in use day to be loaded correctly into the labels from the view state center that we would say something like if need to view state and obviously he is
Minute 3
Property of the use control there s also the same property on a page that contains the view state information so in this case may die to state represents the view state for this particular user control in the view state is a certain type of collection called a hybrid dictionary that contains a name value pairs so in this case for the view state if we want to to add a particular value we would put its name so will test for the member name which is a view state property or item that were going to be storing him will say if me view state member name is nothing meaning that we do not have a member name items in our view state property then that would mean that the view state information is not available and will put in else and here
Minute 4
Which means that the view state information would be available toward him and do it if the view state information is not available is within a simulated loading the information from a database and storing it in the view state s homage to a comment to that effect will save load data from database and store in view state and what we ll do is just hardcode values here for sake of simplicity so we ll go me view state and the first thing that worked in a store is the member name and all sign that to be Joe Smith the second thing there were no store in the view state is an item called the last visit and will set that equal to a daytime celebrities make up the date will just say 1120
Minute 5
2007 had 10 in the morning next thing in the view state will be the total budget has said this is a shopping site so this will retrieve the total budget available to the user from the database and in this case won t put a 500 the next thing we ll get from the database and store in the view state is the items purchased and this is the number of items that the user has purchased a make that equal to 10 and OS X will this play is the amount of money they have spent year to date
Minute 6
This user has been 2500 Sunday thing we ll do is to loot the view state information into the labels on the page and to do that when I m going to do is have to get rid of this else statement and what we ll do is have the if statement just simulated loading the data from the database the first time through and now will take the values from the view state and assign a key to the labels on the page will say label one txt the animal sign that to me view state and it s as simple as typing the name of the key in the view state and in this case converting it to a string and assigning it to the label someone go along here and do that for the rest of the labels that we have the second thing that worked and I have is the last visit
Minute 7
To string and I might go back once I finish this and will make these labels a little more complete so let s take a label through a head that will take the view state value total budget string label for will be the items purchased and the last one label five will be equal to the unity purchase now that will just display those of values in the labels and for some of these we might want a little static text that
Minute 8
Well when you do here is a last visit in head that it will leave the member name has said is in her hair will put the total budget and bowl Ashlee says total available that s what this one represents a bad number of items purchased your items purchased and total purchase of anything we need to use call this lewd member info method somewhere when used control is being constructed
Minute 9
Earning the page processing back on the server and it is important where we call this number to call it in me the pages or user controls load events may put an the load event here and there we go we have a load event now and in this load event will call load member info and other reason is important that we called this in the load event is that you need to understand in the cycle of page processing back on the server when did you stayed he is returned back to the user controls and white when it is saved and when important distinction is that the usage information that already exists is not available in the end it method for the page or
Minute 10
In this case the user control so that if we have put this little member info method in the page_in it for the user control then reviews the information would not be available and we would get behavior that we expected whoever the page load event does occur after the view state information has been restored to the view state property or object in the user control so that s a good place to load this member info in the lesson we need to do is put our use control on the default ASP ex page so will switch over to that go into the design view and I m going to simply drag and drop the user info a STX user control on tour page than the last thing we need on our pages to have a post that said to do that learn to do is add a button to the page walked into the standard controls will get a button and drag
Minute 11
Drop that on tour page now let s does have a page and before I run it let me go over to the code behind for our member info using controlled set to break points the first online 10 witches were simulating the load of the member data from the database and storing it in the view state and the cycle output here on line 17 which is where were assigning to use the information to the various labels from the page or in the use control so now let s start up the webpage and see what happens and as we might expect the first time the page is loading we hit the break point where we re storing the member information from the database into the state will let it run from here next that stops down here where we re loading the view state information into the labels on the page and let it run along and are page does
Minute 12
Displayed the year member related information suggests what happens the first time the page is loaded now let s click the button to have a post back of this page and see what happens click the button and notice this time we bypassed the step we were loading the information into the view state because our test here of whether or not the member name item is in existence in the view state is true because it is there so we skipped the if clause of data loading go right down to loading the information from the view state and this will go along and load the information from each of the items in our view state
Minute 13
Came directly from the view state object let me shut down the page and in this first pass at using the view state what we were doing is sort storing strings in as each and each item of the view state one item per string value is another approach that I want to show you that might be a ore powerful and more versatile and your particular situation sold to a simple demonstration here were going to do is within at a class for a website project and wouldn t call this class member at it via code folder and insight of member we re going to create a public
Minute 14
To these values us over for a slumber didn t do is be_member name as string and then were given to private_last visit as a string and create a call total budget and then we ll create one called items purchased and ashy goodness the total number of items over to make that an integer and total budget will make data and ensure us well in the last thing is usually purchase private_YTD purchase and I will just make that an inch color screen of public property for each the
Minute 15
Is so low public property member name as string and we will return_member name and will set_member name new and annexed property will be less visited Senegal long pre properties for each of these public property and the last visit as a string will return_last visit set_last visit next when its total budget as
Minute 16
Teacher will return_total budget said total budget and we have items purchased in Mexico s public property items purchased that is an integer and will return_items purchased said it and I think you ll probably recognize we re going to go with this is what can actually store the object instead of the individual values Celeste property is used the purchase
Minute 17
10 inch stir in and returned why TT purchase and set it to know we have an object that represents the values that we have for the member and forgot to set a value apparently just do that and check to make sure the other ones look good in which they do so now we can go back over to our class and instead of sitting the individual values See is the member object to be created so a lot of it do is just comment out this part in this part so that when you download the code you ll have that to look at symbol will do here is first book printed in sin
Minute 18
Of our member object soul city game member is instance of the member equals new member Nelson at each of its properties civil saying member member name equal to this time you some different values so will say Jane Doe is a member last visit equal to 12 one to rather to rose 7 and member total budget equal to thousand dollars say member items purchase
Minute 19
Equal to 17 men last Will said Waikiki is equal to 4000 or since a 4000 then will we need to do is reduce me to say me you got few state and called this item member and we ll said that equal to the instance of the member object going down below mobile to his access the values of the member object fully need down here is to get an instance of that member object from the view state so we ll just for simplicity will create another variable colas member info as an member class said that equal to does another use case we have converted
Minute 20
Will say seat type and what can be done view state to 10 member item and convert that to type member and now we have our member object now we just have the label text again missiles and label one text equal to member info to member Nate s little able to do tax equal to the member and throw last visit and won t go along this time of the symptoms of their labels that we had above so we ll say last visit plus member info last visit
Minute 21
Will go label three equal to total available plus member info that was total budget and that s an image teacher so just can say to strain in and we have a label for tax equal to items purchased in Tibet will set that he is a member info to a items purchased to strain and the last one label five text will set equal to the total per
Minute 22
Just and will get the value from the member info objects and said that the strength so this should give us in essentially the same thing that we had before but it s storing a complex object in our view state is one thing that we need to do here and that is we need to make our member class serializable because these state works by taking information serializing it and then basics base 64 encoding it and putting it in the view state and by default or member class for members of the instances of our member class will not be serializable sluts go back over to her member class and let s add that so will set you serializable and now
Minute 23
Member class can be serialized by the view state process will back over to her code behind or use control let s just make sure that we have our breakpoints set you take away the old one left one here when were instantiating the member info object and will put the other one up here where were storing the information first time through in the last thing we need to do is to change the if statement religious will confidence the clipboard in the comment this one out and put that in right above it and if you ve been following along to see that we have to test here for the existence of the member item right rather than the member name item as Phil said to test this out as fired up in the debug mode in the page starts to initialize and then load and first time through he can see where it
Minute 24
Stanch eating our member object and assigning the values to the properties will just add to member name to show that it is doing that now they ll let it proceed down for second breakpoint here we are retrieving the instance of the member object from the view state and converting it to an instance of type member step to that and now we re going to get the member name value from the property of the member info object will step in here hoaxes hover over member name and you can see it does have Jane Doe the information that we put in previously stepped out announcing to do that for the rest labels will just let it run from here outcomes are paid with our values on the member information lets post back click the button go over here to Visual Studio you can see it has skipped the initial
Minute 25
Loading of the information into the member object it s just retrieving our member object right from view state and will step in to member name again and to show that indeed the value chain dough is still inside our member object stepped out will let it run from here and outcomes are page with the member information that was retrieved from the view state so in summary he can see we learn how to use the view state property of the user control to store information first example we stored single values strings inside a different items inside the view state property and then the second time we created a class which recalled member at public properties for each of the values and we stored the data inside an instance of the member class and an intern the member class was stored in the view state property in this encapsulate the encapsulated
Minute 26
The data down into a separate class it is important to remember that that class had to be serializable because that s how the view state works so we headed the serializable attribute to our member class when important thing to add about storing view state in a custom user control is that if you have user controls inside of your use of crushes a server controls inside of your user control remember that ASP net inherently stores the values inside user controls as part of the post back operation for example get a text box whenever the user enters information in a text box that is automatically s persisted by ASP net it s not part of the view state of the page nor the use control so you would and any information that you put in declarative syntax for control would be persisted so it makes them
Minute 27
All here or we had several label controls if we had put the text inside the label as the tax part of the text attribute
Minute 28
You use view state and a custom user control this is Chris Pelz thanks for watching this video and out implement view state in a custom user control or will
Click here to go to original video page
Sunday, August 31, 2008
Video: How Do I: Save and Load View State Information for a Custom Web Server Control
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment