locked
LoadControlState is not getting fired RRS feed

  • Question

  • User-1673333111 posted

    I'm working on a system were the user goes back and forth between pages. When the user is going back to a previously visited page, I want to open it exactly as it was the last time the user was there.

    I implemented this solution http://www.codeproject.com/Articles/7655/Persisting-the-state-of-a-web-page 
    to "fake" a PostBack when the user returns to an already visited page and then load the old ViewState which is saved in the session. 

    It's working fine with asp.net controls, but my custom controls (which normally is working) don't fire LoadControlState :-(
    I'm doing this.Page.RegisterRequiresControlState(this); in OnInit in all the controls.

    All the controls are created dynamically in Page_Load, but if I create the controls in the aspx, LoadControlState is getting fired.

    Btw, I found this thread http://stackoverflow.com/questions/4643964/loadcontrolstate-not-getting-fired
    but it didn't work for me and I don't really understod the solution...

    Please help me, why is not my custom control acting as the asp.nets? 

    Tuesday, December 4, 2012 9:38 AM

Answers

  • User-1673333111 posted

    I solved it by overriding LoadViewState/SaveViewState instead of LoadControlState/SaveControlState.
    I still don't understand why LoadControlState wasn't fired though. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 5, 2012 10:20 AM

All replies

  • User3866881 posted

    KenQ

    KenQ

    Hi,

    First sorry to say without seeing your codes, I just offer you an example from MSDN. And I cannot reproduce your issue;(

    Restores control-state information from a previous page request that was saved by the SaveControlState method.

    The following code example overrides the LoadControlState method in a custom ASP.NET control. When this method is invoked, it determines whether control state was previously saved for the control and, if so, sets the internal property currentIndex to the saved value.

    The OnInit method is overridden to call the RegisterRequiresControlState method on the Page to indicate that the custom control uses control state.

    public class Sample : Control {
        private int currentIndex = 0;
    
        protected override void OnInit(EventArgs e) {
            Page.RegisterRequiresControlState(this);
            base.OnInit(e);
        }
    
        protected override object SaveControlState() {
            return currentIndex != 0 ? (object)currentIndex : null;
        }
    
        protected override void LoadControlState(object state) {
            if (state != null) {
                currentIndex = (int)state;
            }
        }
    } 
    
    Tuesday, December 4, 2012 9:44 PM
  • User-1673333111 posted

    Thank you for your answer.
    Just to be clear. I have no problem getting LoadControlState fired in "normal" postbacks.

    Here is some of my code:

    		protected override NameValueCollection DeterminePostBackMode()
    		{
    			dialogID = (string)Request.Form["ctl00$__DialogID"]; // Försök att tvätta bort ctl00$
    
    			if (dialogID == null)
    				dialogID = Request.Params["dlgid"];
    
    			pageState = (dialogID == null) ? null : pageState = LoadPageState(dialogID);
    
    			NameValueCollection normalReturnObject=base.DeterminePostBackMode();
    
    			if (!IsRestoredPageState)
    				return normalReturnObject; // default to normal behaviour if there is no persisted pagestate.
    
    			if (normalReturnObject != null) {
    				// this is a normal postback, so don't use persisted page state
    				pageState=null;
    				RemoveSavedPageState(dialogID); // clear the page state from the persistence medium so it is not used again
    				return normalReturnObject;
    			}
    
    			RemoveSavedPageState(dialogID); // clear the page state from the persistence medium so it is not used again
    
    			// This method must return something other than null, otherwise the framework does not call
    			// the LoadPageStateFromPersistenceMedium() method and IsPostBack will return false.
    			// Request.Form is an empty object of the correct type to achieve this.
    			return Request.Form;
    		}
    
    		protected override object LoadPageStateFromPersistenceMedium() {
    			if (!IsRestoredPageState) return base.LoadPageStateFromPersistenceMedium(); // default to normal behaviour if we don't want to restore the persisted page state
    			return pageState.ViewStateObject; // otherwise return the ViewStateObject contained in the persisted pageState
    		}
    
            protected override void SavePageStateToPersistenceMedium(object viewState)
            {
                if (dialogID == null) {
                    SessionArea sessionArea = (SessionArea)HttpContext.Current.Session["SessionArea"];
                    dialogID = "" + sessionArea.krut.m_nCurrentDlgID;
                }
                if (dialogID != null)
                    SavePageState(dialogID, new PageState(viewState, Request.Form, Request.Url));
                base.SavePageStateToPersistenceMedium(viewState);
            }

            protected override void OnInit(EventArgs e)
            {
                this.Page.RegisterRequiresControlState(this);
                base.OnInit(e);
            }
    
            protected override void LoadControlState(object savedState)
            {
                this.m_iState = (int)savedState;
            }
    
            protected override object SaveControlState()
            {
                return this.m_iState;
            }



    Wednesday, December 5, 2012 3:35 AM
  • User-1673333111 posted

    I solved it by overriding LoadViewState/SaveViewState instead of LoadControlState/SaveControlState.
    I still don't understand why LoadControlState wasn't fired though. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 5, 2012 10:20 AM