Accessing ControlDesigner from control?

C

Carole MacDonald

I'm trying to do the following:

public class MyControlDesigner : System.Web.UI.Design.ControlDesigner
{
private string m_html = "";
public string Html
{
get { return m_html; }
set { m_html = value; }
}
public override string GetDesignTimeHtml()
{
return m_html;
}
}

[DesignerAttribute(typeof(MyControlDesigner), typeof(IDesigner))]
public class MyControl : System.Web.UI.WebControls.WebControl
{
override void OnInit(EventArgs e)
{
System.ComponentModel.AttributeCollection attributes =
TypeDescriptor.GetAttributes(this);

DesignerAttribute myAttribute =
(DesignerAttribute)attributes[typeof(DesignerAttribute)];

// somehow set the Html property of the designer to a string
}
}

Where I'm stuck is getting an object reference to MyControlDesigner.
Is this possible? The reason I'm trying to do this is I have a bunch
of custom web controls where I want the GetDesignTimeHtml() to return
a simple string that will be different for each control. I'd rather
not create a different control designer for each one since they'd
basically be identical except for the returned string. My first
thought was to derive from DesignerAttribute so I could pass in
another argument with the html string, but that is a sealed class. So
then I tried to access the ControlDesigner to set the string
dynamically. Is this possible?

Thanks,
Carole
 
V

Victor Garcia Aprea [MVP]

Hi Carole,

The code for generating the design-time html of a control should really go
into a control designer and not the control itself. If you want to have a
common control designer for different type of controls while offering a
customized design-time rendering for each one, then you could add such code
in the ControlDesigner.GetDesignTimeHtml method where you could check the
control type that the particular instance of the designer was initialized
with.

Let me know if you need more help on this issue,

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
My profile: http://aspnet2.com/mvp.ashx?vga
 
J

John Saunders

Carole MacDonald said:
I'm trying to do the following:

Where I'm stuck is getting an object reference to MyControlDesigner.
Is this possible? The reason I'm trying to do this is I have a bunch
of custom web controls where I want the GetDesignTimeHtml() to return
a simple string that will be different for each control. I'd rather
not create a different control designer for each one since they'd
basically be identical except for the returned string. My first
thought was to derive from DesignerAttribute so I could pass in
another argument with the html string, but that is a sealed class. So
then I tried to access the ControlDesigner to set the string
dynamically. Is this possible?

Carole, the designer architecture is structured so that the designer may
access the control, but not vice-versa.

Are you aware that you could use the same designer for all of your web
controls? That designer could then output a different string for each
control by checking to see which control type it was designing.

If you need to configure the "simple string" through attributes, then you
could create your own attribute separate from DesignerAttribute, and pass
the string in the attribute constructor. Then, the designer could look for
that attribute and display it in the returned HTML.
 
C

Carole MacDonald

John Saunders said:
Carole, the designer architecture is structured so that the designer may
access the control, but not vice-versa.

Are you aware that you could use the same designer for all of your web
controls? That designer could then output a different string for each
control by checking to see which control type it was designing.

If you need to configure the "simple string" through attributes, then you
could create your own attribute separate from DesignerAttribute, and pass
the string in the attribute constructor. Then, the designer could look for
that attribute and display it in the returned HTML.

Thanks for the tips and suggestions. I was trying to keep things too
simple, I think, in that I didn't want to have to update both the
Control Designer and the Control when I added a new control. But
based on the responses, I've added a check in the Control Designer to
see what type of control it is and output the respective HTML.

Thanks again,
Carole
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top