Re: ViewState Question

From: Andy Smith (f00_at_foo.com)
Date: 03/10/04


Date: Tue, 9 Mar 2004 21:46:28 -0700

here's a simple example control that has a custom style.

public class MyControl : WebControl {

 private Style otherStyle;

 [
 DesignerSerializationVisibility( DesignerSerializationVisibility.Content ),
 NotifyParentProperty( true ),
 PersistenceMode( PersistenceMode.InnerProperty ),
 ]
 public Style OtherStyle {
  get {
   if ( otherStyle == null ) {
    otherStyle = new Style();
    if ( IsTrackingViewState ) {
     ((IStateManager)otherStyle).TrackViewState();
    }
   }
   return otherStyle;
  }
 }

 protected override void LoadViewState( Object savedState ) {
  if ( savedState = null ) {
   base.LoadViewState( null );
   return;
  }

  Object[] myState = (Object[])savedState;
  if ( myState.Length != 2 ) {
   throw new ArgumentException("Invalid ViewState");
  }

  base.LoadViewState( myState[0] );

  if ( myState[1] != null ) {
   ((IStateManager)OtherStyle).LoadViewState( myState[1] );
  }
 }

 protected override Object SaveViewState() {
  Object[] myState = new Object[2];
  myState[0] = base.SaveViewState();
  myState[1] = ( otherStyle != null ) ?
((IStateManager)otherStyle).SaveViewState() : null;
  for ( int i = 0; i < 2; i++ ) {
   if ( myState[i] != null ) {
    return myState;
   }
  }
  return null;
 }

 protected override void TrackViewState() {
  base.TrackViewState();
  if ( otherStyle != null ) {
   ((IStateManager)otherStyle).TrackViewState();
  }
 }

}

__
Andy Smith

"Mark Olbert" <mark@arcabama.com> wrote in message
news:cais40d26khjkoah3dgphc1sfhaf52lf7q@4ax.com...
> Okay, I give up... how do you get a customized descendant of Style to
persist through ViewState??? I
> tried following the online docs, but I'm obviously doing something wrong.
Here're the overrides I
> created in my WebControl class:
>
> protected override object SaveViewState()
> {
> object baseState = base.SaveViewState();
> object closedStyleState = (closedStyle != null) ? ((IStateManager)
> closedStyle).SaveViewState() : null;
> object[] state = new object[2];
> state[0] = baseState;
> state[1] = closedStyleState;
>
> return state;
> }
>
> protected override void LoadViewState( object savedState )
> {
> if( savedState != null )
> {
> object[] state = (object[]) savedState;
> if( state[0] != null ) base.LoadViewState(state[0]);
> if( state[1] != null ) ((IStateManager)
closedStyle).LoadViewState(state[1]);
> }
> }
>
> protected override void TrackViewState()
> {
> base.TrackViewState();
> if( closedStyle != null ) ((IStateManager) closedStyle).TrackViewState();
> }
>
> When I break on the debugger in SaveViewState(), the closedStyle property
(which is a grandchild of
> Style) shows that it's been initialized (i.e., the BackColor is set to
Red, as per my test webpage).
>
> But the line that should be creating closedStyleState just returns...
null.
>
> Suggestions?
>
> - Mark



Relevant Pages

  • Problems with DataGrid
    ... private DataGridControlColumn dataGridTextBoxColumn2; ... protected override void Dispose(bool disposing) ... int rowNum) ...
    (microsoft.public.dotnet.languages.csharp)
  • Problems with datagrid
    ... private DataGridControlColumn dataGridTextBoxColumn2; ... protected override void Dispose(bool disposing) ... int rowNum) ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Custom controls in datagrid
    ... > protected override void Abort(int rowNum) ... > protected override bool Commit(CurrencyManager dataSource, ... > protected override void Edit(CurrencyManager source, ... > CurrencyManager source, int rowNum) ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Custom controls in datagrid
    ... >> protected override bool Commit(CurrencyManager dataSource, ... >> protected override void Edit(CurrencyManager source, ... >> CurrencyManager source, int rowNum) ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: ViewState too large
    ... > You will need to override two methods for your control. ... > protected override object SaveViewState() ... > SaveViewState returns the object that should be placed into viewstate. ...
    (microsoft.public.dotnet.framework.aspnet)