ControlDesigner not invoked on custom control when control is rendered within another custom control

M

Matt Sokol

I have a custom control that has a simple designer (derived from
System.Web.UI.Design.ControlDesigner) associated with it (using the
DesignerAttribute). The overriden GetDesignTimeHtml provides the
design-time display correctly when this control is placed on a web
form.

My problem is I also want to use this control within another custom
control as a child. However, when the parent renders in design-time,
the child control does not use its designer and renders as if in
run-time. I have debugged the designer using another instance of
VS.NET and it is clear that the custom designer is not invoked and the
normal Render method of the child control is called.

Is this a bug or by design? Or am I missing something that will allow
the custom designer to be invoked when the control is being rendered
within another custom control?

Here is a very simple example of what I'm talking about, if you place
both controls on a web form you will see what I mean:

[System.ComponentModel.Designer(typeof(CustomControl1Designer))]
public class CustomControl1 : System.Web.UI.WebControls.Label
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
this.Text = "This is the run-time display.";
base.Render (writer);
}
}

public class CustomControl1Designer :
System.Web.UI.Design.ControlDesigner
{
public override string GetDesignTimeHtml()
{
return "This is the design-time display.";
}
}

public class CustomControl2 : System.Web.UI.WebControls.WebControl
{
protected override void CreateChildControls()
{
System.Web.UI.WebControls.Label label = new
System.Web.UI.WebControls.Label();
label.Text = "Result of CustomControl1 rendering: ";
Controls.Add(label);
CustomControl1 control1 = new CustomControl1();
Controls.Add(control1);
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
EnsureChildControls();
base.Render (writer);
}
}


Thanks,

Matt Sokol
 
J

John Saunders

Matt, I believe that this is by design. Your simple designer would show up
if it were inside of a Panel or Template, but the infrastructure for that
isn't available to us mere mortals.

Now, on the other hand, since it's one of your controls within another of
your controls, perhaps you could expose the render functionality of the
inner designer in such a way that the outer designer could get at it. If the
inner designer had a public static method like "static string
RenderMyControl(MyControl control)", then the outer designer should be able,
during its GetDesignTimeHtml, to find the designer of the inner control and
to invoke this RenderMyControl method to get the HTML.

You'd want to have the inner designer GetDesignTimeHtml call
RenderMyControl, or have them both call common code, just to keep the output
the same between the two of them...
--
John Saunders
Internet Engineer
(e-mail address removed)

Matt Sokol said:
I have a custom control that has a simple designer (derived from
System.Web.UI.Design.ControlDesigner) associated with it (using the
DesignerAttribute). The overriden GetDesignTimeHtml provides the
design-time display correctly when this control is placed on a web
form.

My problem is I also want to use this control within another custom
control as a child. However, when the parent renders in design-time,
the child control does not use its designer and renders as if in
run-time. I have debugged the designer using another instance of
VS.NET and it is clear that the custom designer is not invoked and the
normal Render method of the child control is called.

Is this a bug or by design? Or am I missing something that will allow
the custom designer to be invoked when the control is being rendered
within another custom control?

Here is a very simple example of what I'm talking about, if you place
both controls on a web form you will see what I mean:

[System.ComponentModel.Designer(typeof(CustomControl1Designer))]
public class CustomControl1 : System.Web.UI.WebControls.Label
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
this.Text = "This is the run-time display.";
base.Render (writer);
}
}

public class CustomControl1Designer :
System.Web.UI.Design.ControlDesigner
{
public override string GetDesignTimeHtml()
{
return "This is the design-time display.";
}
}

public class CustomControl2 : System.Web.UI.WebControls.WebControl
{
protected override void CreateChildControls()
{
System.Web.UI.WebControls.Label label = new
System.Web.UI.WebControls.Label();
label.Text = "Result of CustomControl1 rendering: ";
Controls.Add(label);
CustomControl1 control1 = new CustomControl1();
Controls.Add(control1);
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
EnsureChildControls();
base.Render (writer);
}
}


Thanks,

Matt Sokol
 
T

Theo Tillotson

I am dealing with the same issue. Is there any reference to how the
default ControlDesigner that is invoked by MyBase..GetDesignTimeHtml
generates the design-time html?

One alternative I've been thinking about was to ignore the control
during design-time display. Setting it's Visibility to false
temporarally, which is similar to what is done with Validator controls
sometimes in MS examples.

I like Johns idea, and although I haven't tried it, I'm not sure that it
would work since the output would be asymetric to the actual WISYWIG
layout; meaning... that the HTML of the child control would either come
before or after the rest of the parent's design time html. Imbedding it
at the proper location in the stream is the trickiest part of that
idea.... if I'm not just way off base.
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top