Updating Design Time When Property Changes

M

Mark Olbert

I've written a composite custom control which I would like to have update its design-time display when one of several properties
changes at design time. These custom properties are not simply the exposed properties of the constituent controls (e.g., they
control how many constitutent controls are displayed).

I am at a loss as to how to go about doing this. I have a custom designer for the control, but I don't see any functionality in the
base class (ControlDesigner) to "monitor" property changes and then alert the DesignerHost that the control needs to be redrawn.

I am also confused as to why I've just spent 2 hours trying to find references to solving this problem on google, with no
joy...since it seems like it would be a very common problem :). But that's another story.

Any leads or references would be appreciated!

- Mark
 
W

Walter Wang [MSFT]

Hi Mark,

I'm not very clear about your question. For a simple property that needs to
assign to the constituent control, if you do that in CreateChildControls,
you will notice you don't need to do anything to update the designer since
the CreateChildControls will be called again to update the designer when
the property is changed. Here's a test:

1) Suppose we have following simple CompositeControl:


public class Class1 : CompositeControl
{
private Label m_lblName;

public string NameLabel
{
get {
Debug.WriteLine("NameLabel Getter");
return ViewState["NameLabel"] as string;
}
set {
Debug.WriteLine("NameLabel Setter");
ViewState["NameLabel"] = value;
}
}

protected override void CreateChildControls()
{
Debug.WriteLine("CreateChildControls");

Controls.Clear();

m_lblName = new Label();
m_lblName.ID = "lblName";
m_lblName.Text = NameLabel;
Controls.Add(m_lblName);
}
}

2) Use this control in a WebForm and change its NameLabel property, make
sure you have turned on Debug in compilation options; and use DebugView
from sysinternals to view the output. You will notice every time you change
the NameLabel property, the CreateChildControls will get called again.



I understand in your specific case, you're using your own designer, the
behavior will be controlled by the designer. Would you please tell me more
about your custom designer and what's your objective to achieve when using
it?

Based on my understanding, when a property is changed, OnComponentChanged
will be called and you will know a property is changed. On the other hand,
if you changed a property in code and want to update the designer surface,
you need to call this method.


#ControlDesigner.OnComponentChanged Method (System.Web.UI.Design)
http://msdn2.microsoft.com/en-us/library/system.web.ui.design.controldesigne
r.oncomponentchanged.aspx


If in doubt, please send me a simple but complete project to demonstrate
the issue.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Mark Olbert

Okay, it turns out it's so simple I missed it: clear the Controls collection, set ChildControlsCreated to false and call
EnsureChildControls(). Although, for some reason, if you clear the Controls collection while ChildControlsCreated is false,
CreateChildControls() gets called automatically on the Clear()...which means it gets called twice. So you have to test the state of
ChildControlsCreated first.

- Mark
 
M

Mark Olbert

Walter,

Thanx for the quick reply. Since my reply to myself about the solution was pretty incoherent, let me try and explain the problem and
the solution differently.

The state property in question involves the number of child controls to include in the composite control. It's not, therefore, a
property of a child control which could simply be exposed publicly -- changing the property value "radically" changes the makeup of
the composite control.

When a property like the Font of a composite control changes, and you assign the property to the child controls, I guess the change
is propagated through the controls and forces a redraw of the design-time display.

In my case I had to force a recreation of the control hierarchy when the property in question changed. I did that by inserting the
following code block in the property set method:

if( ChildControlsCreated )
{
Controls.Clear();
ChildControlsCreated = false;
EnsureChildControls();
}

I added the test for ChildControlsCreated when I noticed that, without it, the initial setting of the property (i.e., during the
composite control's initialization, when no child controls had been created) caused CreateChildControls() to be called both when I
called Controls.Clear() and when I called EnsureChildControls (because I'd reset ChildControlsCreated after the Clear). I didn't
want to create the control hierarchy twice, hence the inclusion of the test.

- Mark
 
W

Walter Wang [MSFT]

Mark,

Thanks for sharing your solution here. You're right that if a property will
affect the control hierarchy, the property ChildControlsCreated will need
to set to false to force it recreate the control hierarchy again.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top