Page view counter

Custom Control problem with ViewState

Last post 07-21-2008 8:18 AM by mpaterson. 5 replies.

Sort Posts:

  • Custom Control problem with ViewState

    07-19-2008, 2:03 PM
    • Loading...
    • mpaterson
    • Joined on 11-27-2006, 3:24 AM
    • Ipswich, MA
    • Posts 1,264
    • Points 4,541

    I have a custom composite control and am storing a string value in ViewState.
    When I click a button I'm updating it's value to a new string and persisting it to ViewState.
    However, when CreateChildControls() is called the ViewState value is reset.
    This works fine when I use Session but I'd rather avoid that if possible. 

    Any ideas?

    If everything happens for a reason what is the reason for this error?
  • Re: Custom Control problem with ViewState

    07-20-2008, 7:08 AM
    • Loading...
    • gavinharriss
    • Joined on 03-05-2005, 8:07 AM
    • Posts 289
    • Points 873
    Just a guess... you might be setting ViewState values in part of page's lifecycle where it's not getting persisted? Too late?
    Gavin Harriss
    Portfolio: www.gavinharriss.com
  • Re: Custom Control problem with ViewState

    07-20-2008, 7:42 AM
    • Loading...
    • Dr.Osika
    • Joined on 05-05-2008, 9:06 AM
    • Jordan
    • Posts 108
    • Points 610

     Hi,

    You can access the viewstate value on the Prerend event

     

    Osama Al Fares
    My Linked In Profile



    ** Please Mark the reply as Answer if it helps you to solve your problem **
  • Re: Custom Control problem with ViewState

    07-20-2008, 10:42 AM
    • Loading...
    • mpaterson
    • Joined on 11-27-2006, 3:24 AM
    • Ipswich, MA
    • Posts 1,264
    • Points 4,541

    I'm setting these viewState values during a button click event.
    Is this too late?

    The click event fires, then in preRender CreateChildControls() is being called.
    It is in CreateChildControls() that I need the updated ViewState values.

    If everything happens for a reason what is the reason for this error?
  • Re: Custom Control problem with ViewState

    07-21-2008, 4:14 AM
    • Loading...
    • My Crystal
    • Joined on 06-08-2008, 2:09 PM
    • Guangzhou, China
    • Posts 320
    • Points 1,313

    hi,mpaterson

    The button clicked is within your custom control.Right?

    I don't have any detailed idea about the cause of your problem. But one basic rule--make sure the viewstate you saved is not reset by your custom control's code in the later stages of the custom control's lifecycle.

    Do you mind post the custom control's code?

    What really matters most is the chance to communicate with you, my friends, rather than marking my post as answer, though I would be really appreciated if you do so.

    ASP.NET 3.5 MCTS
  • Re: Custom Control problem with ViewState

    07-21-2008, 8:18 AM
    • Loading...
    • mpaterson
    • Joined on 11-27-2006, 3:24 AM
    • Ipswich, MA
    • Posts 1,264
    • Points 4,541

    No the button click is not in this custom control... it is in another, related custom control.
    I have two custom controls (1) ModeLinkButton and (2) EditableTextBox

    The concept is pretty simple:
    You can register multiple instances of the EditableTextBox to a ModeLinkButton as listeners.
    When you click "Edit" in ModeLinkButton it will tell all of its subscribers (ie. EditableTextBoxes) to render as textboxes.
    Otherwise, EditableTextBox will render as a label. 


    Here is the click event from ModeLinkButton

    void LinkButton_Click( object sender, EventArgs e )
            {
                LinkButton lb = ( LinkButton )sender;
    
                switch( lb.CommandName )
                {
                    case "View":
                        this.State = Enums.State.Edit;
    
                        foreach( IEditControl c in this.Subscribers )
                            c.State = Enums.State.Edit;
    
                        break;
    
                    case "Cancel":
                        this.State = Enums.State.View;
    
                        foreach( IEditControl c in this.Subscribers )
                            c.State = Enums.State.View;
    
                        break;
    
                    case "Apply":
                        this.State = Enums.State.View;
    
                        foreach( IEditControl c in this.Subscribers )
                            c.State = Enums.State.Apply;
    
                        break;
    
                    default:
                        throw new ApplicationException( "Invalid LinkButton CommandName." );
                }
            }

     

    Here is the EditableTextBox State property:

    [DefaultValue( Motivano.Constants.Enums.State.View )]
            public Motivano.Constants.Enums.State State
            {
                get
                {
                    if( HttpContext.Current.Session[ this.ID + "_State" ] == null )
                    {
                        this.State = Motivano.Constants.Enums.State.View;
                    }
    
                    return ( Motivano.Constants.Enums.State )HttpContext.Current.Session[ this.ID + "_State" ];
                }
                set
                {
                    HttpContext.Current.Session[ this.ID + "_State" ] = value;
    
                    if( value == Motivano.Constants.Enums.State.Apply )
                    {
                        Update( );
                    }
                }
            }

     As you can see in the setter, if the state is "Apply" then we call the EditableTextbox controls Update() method:

    private void Update( )
            {
                if( this.Txt != null )
                    this.Value = this.Txt.Text;
    
                this.State = Motivano.Constants.Enums.State.View;
            }

     The Update() method simply grabs the value from the textbox and plops it into the Value property:

    [DefaultValue( "" )]
            public string Value
            {
                get
                {
                    if( ViewState[ this.ID + "_Value" ] == null )
                        this.Value = String.Empty;
    
                    return ViewState[ this.ID + "_Value" ].ToString( );
                }
    
                set
                {
                    ViewState[ this.ID + "_Value" ] = value;
                }
            }

     So anyhow, let me know if you see anything that is sticking out!

    Thanks

    If everything happens for a reason what is the reason for this error?
Page 1 of 1 (6 items)