Composite controls not visible at design time

  • Thread starter Peter Morris [Droopy Eyes Software]
  • Start date
P

Peter Morris [Droopy Eyes Software]

Hi all

I have a few questions regarding composite controls

1) My composite controls are created based on a property of my control.
When the postback of a page occurs due to a button click, my
CreateChildControls() method is called before the button code is executed,
so if that button code changes the property in question it is already too
late to reflect this in the control. Is there a way to "Invalidate" my
control when this property is set so that it's child controls are recreated?

2) Dropping my component on to a WebForm reveals two behaviours which I do
not want.
2a) Firstly the control has a specific width/height, whereas I want it
to size automatically based on which of the child controls are visible.
2b) At design time, none of the child controls are visible at all

3) My final question. One property of my control is a DateTime. If I allow
data binding to this property how do I handle a null value from the data
source? I have an IsNull property, but surely the data binding will try to
bind the DateTime to null?

Thank you
 
J

John Saunders

Peter Morris said:
Hi all

I have a few questions regarding composite controls

1) My composite controls are created based on a property of my control.
When the postback of a page occurs due to a button click, my
CreateChildControls() method is called before the button code is executed,
so if that button code changes the property in question it is already too
late to reflect this in the control. Is there a way to "Invalidate" my
control when this property is set so that it's child controls are
recreated?

Yes. Set ChildControlsCreated to false.
2) Dropping my component on to a WebForm reveals two behaviours which I do
not want.
2a) Firstly the control has a specific width/height, whereas I want it
to size automatically based on which of the child controls are visible.

That doesn't always happen to me. What is your control derived from? Does it
set its own size?
2b) At design time, none of the child controls are visible at all

Composite controls should have a designer. In the GetDesignTimeHtml method,
they should cause EnsureChildControls to be called. The basic pattern is as
follows (note that I haven't tried to build this):

namespace MyControls
{
[Designer(typeof(Design.CompositeControlDesigner))]
public class CompositeControl : WebControl, INamingContainer
{
protected override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}

public string PropertyThatDeterminesChildren
{
get {return (string) ViewState["propertyThatDeterminesChildren"];}
set
{
ViewState["propertyThatDeterminesChildren"] = value;
ChildControlsCreated = false;
}
}

private Label _childLabel;

public string PropertyWhichRequiresChildren
{
get
{
EnsureChildControls();
return _childLabel.Text;
}
set
{
EnsureChildControls();
_childLabel.Text = value;
}
}

protected override void CreateChildControls()
{
// Create your control hierarchy here
_childLabel = new Label();
_childLabel.ID = "childLabel";
base.Controls.Add(_childLabel); // Note: don't recurse

if (PropertyThatDeterminesChildren == "button")
{
Button btn = new Button();
btn.ID = "btn";
btn.Text = "Button";
base.Controls.Add(btn);
} else {
LinkButton lnk = new LinkButton();
lnk.ID = "lnk";
lnk.Text = "LinkButton";
base.Controls.Add(lnk);
}
}
}
}

namespace MyControls.Design
{
public class CompositeControlDesigner : ControlDesigner
{
protected override string GetDesignTimeHtml()
{
// Ensure that the controls are created
ControlCollection controls = ((CompositeControl)
Component).Controls;

// ... then
return base.GetDesignTimeHtml();
}
}
}

Your own controls would then derive from CompositeControl.
3) My final question. One property of my control is a DateTime. If I allow
data binding to this property how do I handle a null value from the data
source? I have an IsNull property, but surely the data binding will try to
bind the DateTime to null?

When you say "null", do you mean a NULL from a database? If so, then an
exception will be thrown when an attempt is made to bind the NULL to your
DateTime property. The same will happen no matter what kind of property you
bind to, since whatever type it is (except for object), it isn't DBNull,
which is a distinct type. This is no different from any other control, so
your callers should probably do with this DateTime property whatever they
would do with a string property.
 
P

Peter Morris [Droopy Eyes Software]

Hi John

Thanks for the excellent level of detail.

That doesn't always happen to me. What is your control derived from? Does it
set its own size?

System.Web.UI.WebControls.WebControl, INamingContainer

It doesn't set its own size in the constructor or anything.

Seeing as I will need to do this again in the future, and will most likely
forget the details, do you mind if I post this information on my website
(where I keep all information I don't want to forget)? I'd be perfectly
happy to credit the article in your name, and provide a URL link of your
choice.

Thanks again!


--
Pete
====
Audio compression components, DIB graphics controls, FastStrings
http://www.droopyeyes.com

Read or write articles on just about anything
http://www.HowToDoThings.com
 
J

John Saunders

Peter Morris said:
Hi John

Thanks for the excellent level of detail.

You're welcome, and I hope it actually helped!
Does

System.Web.UI.WebControls.WebControl, INamingContainer

It doesn't set its own size in the constructor or anything.

Strange. The control I'm most recently working on is a composite control
which renders a Button. It's quite happy to take up only the size of the
(textless) button.
Seeing as I will need to do this again in the future, and will most likely
forget the details, do you mind if I post this information on my website
(where I keep all information I don't want to forget)? I'd be perfectly
happy to credit the article in your name, and provide a URL link of your
choice.

Go ahead and post it wherever you like. I figure once it gets posted on a
newsgroup, the world owns it.

I'd suggest you include either a URL to this newsgroup discussion, or at
least a textual reference to it. That way, readers of your web site will be
able to find the discussion.
 
P

Peter Morris [Droopy Eyes Software]

You're welcome, and I hope it actually helped!

Yes, it did. It solved all of the problems I was experiencing except the
size issue.
Strange. The control I'm most recently working on is a composite control
which renders a Button. It's quite happy to take up only the size of the
(textless) button.

I remember seeing a property in the designer about disallowing resize or
something, I will try that later and see if it solves the problem.

Go ahead and post it wherever you like. I figure once it gets posted on a
newsgroup, the world owns it.

Thanks. I'll write it up later.

I appreciate your help!
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top