If you find that your ViewState is unavailable to your user control in ASP.Net and if you loaded the user-control dynamically (using the LoadControl method), then check to make sure that you are dynamically loading the user-control in the OnInit method. Most people make the mistake of loading the user-control in the Page_Load method and the problem with that is that the state is loaded before the Page_Load method is called (see diagram below – Page_Load is the Load event). When the view state is loaded and processed, ASP.Net will look for the control and if it is there – it will fill it with the data from the view-state and if the control isnt available, then it will be ignored.
By loading the user-control in the Init event, you ensure that the control will be available when the LoadViewState method is called by the framework and it will then be filled out correctly with the values from the view-state. (This is usually followed by the Raise xxxx Events, where you typically check the value of the control to perform further processing).
If you find that the LoadViewState method is not being called, then it is most probably because your control it-self is not using the View-State. To ensure that it is called try and over-ride the SaveViewState method and save a dummy value to the view-state. After this the LoadViewState should begin getting called.
No comments:
Post a Comment