Complex properties (Button) not persisting in webcontrol

P

Phillyboy123

This seems to be a common issue and one that I have not found a
solution to. What I want to do is theoreticly simple. I have tried
creating webcontrols with simple properties string e.t.c and all work
fine. I am now in the process if trying to create a webcontrol that
contains more complex properties. In this case a Property of type
Button. I've played around with all of the recomended metadata
ParseChildren(true/false), PersistChildren(true/false),
PersistanceMode e.t.c. I've also been using "Developing Microsoft
ASP.NET server controls and components" by Nikhil Kothari and Vandana
Datye" but with no success in this area.

At designtime the control is rendered on the page correctly and the
Properties of the Button within the custom control appear in the
properties toolbar/palette. Changes to the Buttons properties ie. Text
or BackColor seem to take effect when the page is redrawn however
these properties do not persist.

1. There values are lost when the project is rebuilt or ran.
2. The HTML for the aspx page has not changed to reflect the new
property values.

Below is the code for the class.

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI.Design;

namespace DynamicWebControls.Controls
{
public class TestControl:
System.Web.UI.WebControls.WebControl,INamingContainer
{
Button _myButton;

protected override void CreateChildControls()
{
Controls.Clear();
Controls.Add(MyButton);
}

[
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(true),
PersistenceMode(PersistenceMode.InnerProperty)
]
public Button MyButton
{
get
{
EnsureChildControls();
if(_myButton==null)_myButton=new Button();

return _myButton;
}
set
{
_myButton = value;
}
}

protected override void Render(HtmlTextWriter writer)
{
MyButton.RenderControl(writer);
}
}
}

Thanks in advance for your help.

Phil.
 
S

Sam Fields

Correct me if I'm wrong, but it seems that you're just looking to customize
the <asp:Button>. You may want to consider inheriting the button class
instead of wrapping it.

Thanks,

Sam Fields
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top