Ver Mensaje Individual
  #2 (permalink)  
Antiguo 25/04/2008, 12:08
Avatar de Peterpay
Peterpay
Colaborador
 
Fecha de Ingreso: septiembre-2007
Ubicación: San Francisco, United States
Mensajes: 3.858
Antigüedad: 17 años, 5 meses
Puntos: 87
Re: Ayuda con creacion de control

Ok mira eso es un tanto complicado aca te pongo un ejemplillo q hice hace tiempo considerando un StateBag, utilizar las interfaces de IStateManager y clases parciales para separar el manejo del state del manejo de las propiedades y el control web.

Si tus propiedades son simple texto usa esto

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
[DesignerSerializationVisibility( DesignerSerializationVisibility.Visible)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}

set
{
ViewState["Text"] = value;
}
}



------------------------------------

si son propiedades complejas usa esto

------------------------------ control con propiedad compleja -------------------------------
[ToolboxData("<{0}:TestControl runat=server></{0}:TestControl>")]
public partial class TestControl : WebControl
{
#region VS AutoGenerated Code

protected override void RenderContents(HtmlTextWriter output)
{
output.WriteLine(Text);
output.WriteLine("BookInfo Property:" + this.BookInfo.BookName);
}

#endregion

#region Custom Properties
private BookInfo bookInfo;
[Browsable(true)]
[Category("Custom")]
[DesignerSerializationVisibility( DesignerSerializationVisibility.Content)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public virtual BookInfo BookInfo
{
get
{
if (bookInfo == null)
{
bookInfo = new BookInfo();
if (IsTrackingViewState)
{
((IStateManager)bookInfo).TrackViewState();
}
}
return bookInfo;
}
}

#endregion

#region ViewState for Custom Properties
protected override void LoadViewState(object savedState)
{


if (savedState != null)
{
object[] states = (object[])savedState;
base.LoadViewState(states[0]);
((IStateManager)BookInfo).LoadViewState(states[1]);
}
}

protected override object SaveViewState()
{
object[] states = new object[2];
states[0] = base.SaveViewState();
states[1] = bookInfo != null ? ((IStateManager)bookInfo).SaveViewState() : null;
return states;
}

protected override void TrackViewState()
{
// for each property you add you will need to add another if to this funtion following the same schema
if (bookInfo != null) //Composite Property
{
((IStateManager)bookInfo).TrackViewState();
}
base.TrackViewState();
}
#endregion


}


---------------------------------------- propiedad compleja con soporte de viewstate
public partial class BookInfo
{
public enum ShipDay { Sunday,Monday,Saturday };


public BookInfo()
{
this.BookName = string.Empty;
this.ShipmentDay = ShipDay.Monday;
}


[NotifyParentProperty(true),RefreshProperties( RefreshProperties.Repaint)]
public virtual string BookName
{
get
{
string tempo=ViewState["BookName"].ToString();
return string.IsNullOrEmpty(tempo) ? string.Empty : tempo;
}
set
{
ViewState["BookName"] = value;
}
}
[NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
public virtual ShipDay ShipmentDay
{
get
{
ShipDay tempo =(ShipDay) ViewState["ShipmentDay"];
return tempo;
}
set
{
ViewState["ShipmentDay"] = value;
}
}

protected virtual StateBag ViewState // esta es la clave para salvar el state de tu contro customizado
{
get
{
if (_viewState == null)
{
_viewState = new StateBag(false);

if (_isTrackingViewState)
{
((IStateManager)_viewState).TrackViewState();
}
}
return _viewState;
}

}
}

------------------- la otra mitad de la clase q se encarga de la admon del viewstate

[TypeConverter(typeof(ExpandableObjectConverter))]
public partial class BookInfo:IStateManager
{
private bool _isTrackingViewState;
private StateBag _viewState;


#region IStateManager implementation
bool IStateManager.IsTrackingViewState
{
get
{
return _isTrackingViewState;
}
}

void IStateManager.LoadViewState(object savedState)
{
if (savedState != null)
{
((IStateManager)ViewState).LoadViewState(savedStat e);
}
}

object IStateManager.SaveViewState()
{
object savedState = null;

if (_viewState != null)
{
savedState =
((IStateManager)_viewState).SaveViewState();
}
return savedState;
}

void IStateManager.TrackViewState()
{
_isTrackingViewState = true;

if (_viewState != null)
{
((IStateManager)_viewState).TrackViewState();
}
}
#endregion

internal void SetDirty()
{
_viewState.SetDirty(true);
}

}



------------------------------

Bueno cabe mencionar que el manejo de las propieades como virtuales es para q la implementacion del control permita ver los cambios de las propiedades en design time ademas del runtime.

cualquier pregunta no dudes en enviarla
__________________
Curso WF4
http://cursos.gurudotnet.com/ DF
Aprende HTML5