Child controls disappearing

K

Kelly

I've created a WebControl (that inherits from WebControl) that has
child controls that need to be persisted in the HTML. They seem to be
rendered initially at design-time, but then if I change something
about the parent control, all my inner controls are lost. I've seen
numerous other posts about this issue, but none of the solutions seems
to work. I've tried every combonation of ideas and I'm still running
into the same problem. If anyone has had success with this any advice
would be greatly appreciated!

Here is my parent control: (some code is omitted)

[ToolboxData("<{0}:NotesButton runat=server></{0}:NotesButton>"),
Designer(typeof(NotesButtonDesigner)),
ParseChildren(false),
PersistChildren(true)]
public class NotesButton : WebControl, IPostBackDataHandler
{
private NotesLineItem _line1 = new NotesLineItem();

[PersistenceMode(PersistenceMode.InnerDefaultProperty),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public NotesLineItem Line1
{
get { return _line1; }
set { _line1 = value; }
}

}

//Here is the child
[TypeConverter(typeof(ExpandableObjectConverter))]
public class NotesLineItem
{
private string _label = string.Empty;
private string _value = string.Empty;

[NotifyParentProperty(true)]
public string Label
{
get { return _label; }
set { _label = value; }
}

[NotifyParentProperty(true)]
public string Value
{
get { return _value; }
set { _value = value; }
}
}
 
O

Oliver

Hi Kelly,

Not sure, but is this the problem that Denis Bauer's free control is meant
to solve?

http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

"Problem:

ASP.NET gives a developer the opportunity to programmatically add controls
to a web form using ParentControl.Controls.Add(new Control());
However, these controls are not persisted in any way thus having to be
recreated for each subsequent request.

Goal:
To create a control that behaves like a placeholder but additionally handles
recreating dynamic controls on subsequent requests.

Procedure:
I have created a custom control called DynamicControlsPlaceholder that
derives from Placeholder and overrides Load- and SaveViewState.
In SaveViewState, the control hierarchy is recursively traversed and the
control type and ID persisted to a string
In LoadViewState the persisted information is used to recreate the control
tree to the state before."
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top