Maintaining viewstate in custom control collection

  • Thread starter Patrick Wilby via .NET 247
  • Start date
P

Patrick Wilby via .NET 247

From: Patrick Wilby

Hi (this is the second time I've tried to post this message,apologies if the original is now up on the forum)

I have a custom control (inherited from WebControl) that has aproperty which is a collection (inherited from BaseCollection).This collection holds custom controls of type TabPage(inheritedfrom Control). I can set the Caption property for the TabPageobjects once placed onto a web page (the same functionality as aTextBox.Text property). The problem is that the Caption propertyof the TabPage objects are not retained in viewstate on postbackif they are held in the collection. If I place a standaloneTabPage control onto a page and postback the TabPage controlCaption property retains state (I implemented theIPostBackDataHandler interface in TabPage to do this). So howcan I maintain the viewstate of my TabPage objects whilst beingpart of a collection of another custom object? Should I use theIStateManger interface? How would this be implemented i.e. whichobject should it be implemented in, the class that defines mycollection, the TabPage class or the top level custom controlclass, do I need to implement IPostBackDataHandler in my toplevel custom control class? This is my first custom control anda simple code example (VB preferably) and/or explanation of whatto do would be very useful.

Thanks

Patrick
 
J

Juliet Choy [MVP]

Have you ensure that your Collection has put into the ViewState
collection of your control and made your Collection as Serializable
(since you didn't mention in your post)? This is crucial to ensure that
the properties of your control retain in the ViewState.

The best place to retrieve this information from the ViewState, on the
other hand, should be done in the LoadViewState() method. Your should
override that in your own control.

Hope this help you.

Regards,
Juliet Choy
Hong Kong
Microsoft MVP - ASP.NET
From: Patrick Wilby

Hi (this is the second time I've tried to post this message, apologies if the original is now up on the forum)

I have a custom control (inherited from WebControl) that has a property which is a collection (inherited from BaseCollection). This collection holds custom controls of type TabPage(inherited from Control). I can set the Caption property for the TabPage objects once placed onto a web page (the same functionality as a TextBox.Text property). The problem is that the Caption property of the TabPage objects are not retained in viewstate on postback if they are held in the collection. If I place a standalone TabPage control onto a page and postback the TabPage control Caption property retains state (I implemented the IPostBackDataHandler interface in TabPage to do this). So how can I maintain the viewstate of my TabPage objects whilst being part of a collection of another custom object? Should I use the IStateManger interface? How would this be implemented i.e. which object should it be implemented in, the class that defines my collection, the TabPage class or the top level cust
om control class, do I need to implement IPostBackDataHandler in my top level custom control class? This is my first custom control and a simple code example (VB preferably) and/or explanation of what to do would be very useful.
 
T

Teemu Keiski

Hi,

IStateManager is the way so that what you put in ViewState is optimized as
well as strictly controlled. Basically the chain goes such that those
classes/objects which do not have built-in state management capability
(Control classes have such state management by default via ViewState
collection) would implement IStateManager.

In addition to this, control containing a class/collection having
IStateManager implemented would need to override its
LoadViewState/SaveViewState/TrackViewState initiate saving/loading/tracking
state of the implementing class (i.e control calls IStateManager methods to
initiate collection state management, and collection again forwards this to
the objects it stores, means that collection items can also implement
IStateManager or optionally have their own custom ViewState collection)

I wrote an example about IStateManager to ASP.NET Forums some time ago:
http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=225520

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke




From: Patrick Wilby

Hi (this is the second time I've tried to post this message, apologies if
the original is now up on the forum)

I have a custom control (inherited from WebControl) that has a property
which is a collection (inherited from BaseCollection). This collection holds
custom controls of type TabPage(inherited from Control). I can set the
Caption property for the TabPage objects once placed onto a web page (the
same functionality as a TextBox.Text property). The problem is that the
Caption property of the TabPage objects are not retained in viewstate on
postback if they are held in the collection. If I place a standalone TabPage
control onto a page and postback the TabPage control Caption property
retains state (I implemented the IPostBackDataHandler interface in TabPage
to do this). So how can I maintain the viewstate of my TabPage objects
whilst being part of a collection of another custom object? Should I use the
IStateManger interface? How would this be implemented i.e. which object
should it be implemented in, the class that defines my collection, the
TabPage class or the top level custom control class, do I need to implement
IPostBackDataHandler in my top level custom control class? This is my first
custom control and a simple code example (VB preferably) and/or explanation
of what to do would be very useful.

Thanks

Patrick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top