In this video Chris Pels will show how to store information related to a custom web server control in the control state which is independent of the ViewState information for the control. Unlike ViewState the control state can not be turned off and is therefore a more reliable way to store important information for a control. A discussion of what types of information are appropriate to store in control state is also included.
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
Hi my names Chris tells in his video when you learn how to implement control state and foreign ASP net user control of control state is much like you stating that it s a way to persist information related to the use control during post backs to the server whoever controls it is intended to be used to persist information related to the behavior of the control is used is intended to be used to persist information related to the user interface aspect plus get started by creating a website with a new website which I m going to call website DB and now put this in a sample code folder so when you download the sample code that comes with the video I have it and can work with it presently want to do is add a user control and in the user control is where would I put the functionality to save and restore the control state let s just call this sample control
Minute 1
And in this user control and we want to have at least a little bit of user interface so that you can see the user interface only at the control to a page and the intent of this user control is to store customer information and will be doing is us saving and restoring the unique identifier for the customer and control state solicitors put a couple controls on Tuesday user control design surface someone at a text box and it lets you add a label as well label and put them onto the design surface and it is a move that and let me just put this label their lets change a property or to
Minute 2
Messages but over here for the label go to the label and its coldest so customer name and that ll be good enough to be spin will see the user control on the webpage to know let s go to the code behind in the user control this is where I do most of our work now we need to add three things in order for this user control to save and restore control state and the first thing we need to edit our two methods an override to existing methods want to save the control state and one to load the controls it so but said the save control statements here first is that there is it controls the analyst said the load control state
Minute 3
Know we have two methods that see it and restore the control state and inherently a user control does not invoke the control state functionality so we have to indicate that by calling on method and the best place to call that method would be in the end it method for the controls so let s said that on and it down here in on it we have to do to invoke the control state functionality is go to the page object and you have to go to a method called register requirements register requires control state and then
Minute 4
Too bad we have to pass a reference to the user controls or just like me and that will get the control state functionality going here in our user control and we want to save and restore in the control state is the unique identifier for the customer so let me just add a private variable called_customer ID as an integer and that leaves wearables for the primary key for the customer that s currently in the user control and let me at a public property exposed that customer ID that will also be an integer and we would return the private cargo and said at a quarter
Minute 5
And so that s the value that we want to save and restore in our controls they must now go down into the control state methods and personal to save what we need here is a way to store the value and you ll notice here that we also have a currently it s has returned my base save control statements about to say that this cool site of the base class and in the state controls to nothing but you need to do is as a return value you need to pass the object that you want the control state is safe for you so organa do hear his changes around a little bit and her family to its convention a variable which will just call control state has sent an array of objects and then we ll going to do it
Minute 6
Is taken this and rather than return it and what can it do is produce a control state and in the first element of the control state organists Dorothy controls the information for the base class and then in the second almond were going to store is our customer ID value and then when we re done with that he will appoint a return is the control state variable or object which will contain both the base class controls the information as well as our customer ID so that gives us us to safety controls the information that we want now let s go down to the lewd controls they had recently wanted to the inverse of what we didn t save control state so the first thing I do is to mention a local variable
Minute 7
Control state as an object and McGuinness said that equal to this inbound safety state parameter which is the state information and we want to change that to an object s older lessee tied to a safe state and convert it to an object and then the next that we want to do is in the base class load the control states rather than passing a safe state will want to pass here is the first element of our object array that contains the base class state information and then what we need to do it is to our customer ID value what we want to assign is the control state the second element of the array and then needs to be an integer so
Minute 8
Let s convert that to a nature so that shouldn t elude the control said information into the user control so we looked to be all set here in terms and planting a save and load control site functionality lets open a bar or default aspx and what we want to do it here in order to test this out is to take our user control the example control drag and drop it onto the webpage and there we have our user control that we need to add a couple more elements here to test this out and the first one that I want to add is a label and what can I do with this label is simply assign the value of the customer ID so that we can have an indicator that the user control is properly saving and restoring the control state and let me go back up to
Minute 9
Assigned service to customers one more thing that we want to put on here and that is a button so that we can use a button to post back to cause the page to post back so that looks good for the default ASP ex page let s go to the code behind and wanted to be and here is when the page loads we want to get the value of a customer ID and put it into that label control so we can see what value is so we ll do that in the low defense let me at an event handler for load and then we ll were going to do here in the load event are actually two different things in the first one is that we re going to take the label one text and two that were going to assign to it the sample control
Minute 10
customer ID now having compiled as if to see customer ID property from that user controlled on a type in because we know it s there and then the next thing that we want to do is intimate to a customer idea but it does compile this so we get rid of that era and the next thing we wanted to his take the sample control customer ID and we just want to increment it to say plus he calls one to what this will do is whenever the page loads it s going to display the value of a customer ID in our label and then it will increment to storing it to the user control and user control with this works according to plan will save the incremented value in the control so that next time we come into the page it should load that incremented value so the page should show us 012 etc
Minute 11
Before we test this out let s go over into the sample code user control and let s just put two break points won here in a safe control state and one in the load control state is let s start up a website and look at our sample page and the initial value should be zero and asked him to come in to save control state the first time the page is loaded onto slip that run here s our page with a zero which is the first value of our customer ID since we by default and assign anything to its old post the page back by clicking our button and let s hop over to Visual Studio It has a breakpoint in load control state is the page is being loaded and if we step down to hear the first thing it does is load the base class control state and then it s going to load our customer ID and let s just hover over customer ID and as you can see it
Minute 12
You is one which is incremented value that we stored on in the load event for the page to let it run from here and here we are coming in to save control state and is going to store the incremented value which in this case should be to select its let it go through and customer ID and tedious to solicit a store that in to the control state will let it run from here and you can see that the value and is now one on the page when it was zero previously and if you continue to post the page we would see this increment two to three and so on to senior out to implement the saving and restoring of control state and in this particular example were storing a single value in
Minute 13
Is the customer IT and you can store anything in the control state because it saves and restores an object and one additional coach that you take when you get to sample and suck implemented in your own code is rather than storing a single value such as customer IDs create a separate class let s say we created a customer class that had multiple properties to it and then we could store that caught in an incidence of that customer class in the control state which will provide us with a lot of flexibility to store multiple values so encourage you to download the source code that comes with this video take a look at the sample man and extended from there to meet the needs of the your particular situation as is Chris Pelz thanks for watching this video on how to implement control state in an ASP net user control
Click here to go to original video page
Sunday, August 31, 2008
Video: How Do I: Use Control State to Persist Information for a Custom Web Server Control
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment