CustomControl on webform in designer shows only text

P

Pipo

Hi,

I've made a customcontrol (inherits from system.web.ui.webcontrol)
Everything works fine.

But when I make my customcontrol...and I reference it in an other project
place the control in the toolbox
and drag it on to a webform, I doesnt see my control but text something like
"MyCustomControlsName"

I accpected to see a textbox and a label instead of the text.
When I run the webapplication I see the textbox and label correct.

Why not in the designer?
How can I show my controls on the designer as well?

thanks alot.
 
B

Brian Scott

Hi,

Sound's like you have a composite control with a TextBox and Label control
inside. These types of controls can have prolems rendering themselves at
designtime without a little help. The control's Render method is probably
being called before the Controls collection is created, which is where your
TextBox and Label exist.

If it's a simple enough control that doesn't require a lot of initialization
try adding a call to EnsureChildControls() in each of your control's
properties like the following.

[Description("..."), Category("...")]
public string Text
{
get
{
EnsureChildControls();
object text = ViewState["text"];
if (text == null)
return "";
else
return (String) text;
}
set
{
EnsureChildControls();
textBox1.Text = value;
ViewState["text"] = value;
}
}

What you are doing here is causing your custom control to create it's
Controls collection when you make a call EnsureChildControls(). This in turn
creates your TextBox and Label and they should then render correctly.

If this doesn't work and you still get the same default text rendering at
design time, you probably need to create a custom designer. Custom designers
inherit from the System.Web.UI.Design.ControlDesigner class. They assist
your composite controls in correctly rendering themselves. If the above
doesn't work let me know. I'll assist you in creating a custom designer.
It's not as complicated as it may sound, so don't worry :D
 
R

Robert Koritnik

What you're missing is a custom control designer class that should be used
for design time control rendering and work with... MSDN should be of enough
help. For the kind of a control that you have it's rather simple to make
one.
 
P

Pipo

Thanks for helping Brain,

I helped but when I put the control on my form the first time its still
text.
When I drag it to an other place on the form the control looks fine!

Can you help me with the custom designer?
I tried the MSDN first but I think I dont understand it....(yet anyway)


thanks again!

Can you help me with

Brian Scott said:
Hi,

Sound's like you have a composite control with a TextBox and Label control
inside. These types of controls can have prolems rendering themselves at
designtime without a little help. The control's Render method is probably
being called before the Controls collection is created, which is where
your
TextBox and Label exist.

If it's a simple enough control that doesn't require a lot of
initialization
try adding a call to EnsureChildControls() in each of your control's
properties like the following.

[Description("..."), Category("...")]
public string Text
{
get
{
EnsureChildControls();
object text = ViewState["text"];
if (text == null)
return "";
else
return (String) text;
}
set
{
EnsureChildControls();
textBox1.Text = value;
ViewState["text"] = value;
}
}

What you are doing here is causing your custom control to create it's
Controls collection when you make a call EnsureChildControls(). This in
turn
creates your TextBox and Label and they should then render correctly.

If this doesn't work and you still get the same default text rendering at
design time, you probably need to create a custom designer. Custom
designers
inherit from the System.Web.UI.Design.ControlDesigner class. They assist
your composite controls in correctly rendering themselves. If the above
doesn't work let me know. I'll assist you in creating a custom designer.
It's not as complicated as it may sound, so don't worry :D

Pipo said:
Hi,

I've made a customcontrol (inherits from system.web.ui.webcontrol)
Everything works fine.

But when I make my customcontrol...and I reference it in an other project
place the control in the toolbox
and drag it on to a webform, I doesnt see my control but text something
like
"MyCustomControlsName"

I accpected to see a textbox and a label instead of the text.
When I run the webapplication I see the textbox and label correct.

Why not in the designer?
How can I show my controls on the designer as well?

thanks alot.
 
S

Steven Cheng[MSFT]

Hi Pipo,

As for defining a Custom ControlDesigner for a custom webcontrol, we can
follow the following steps:

1. Create a custom controldesigner, for example:

========Control Designer class=============
public class TestControlDesigner : System.Web.UI.Design.ControlDesigner
{

public override string GetDesignTimeHtml()
{
string html = "";
html = "<center><div>This is the design-time html!</div></center>"

return html;
}
.....................

}

# The "GetDesignTimeHtml" method just help to provide the html that will
be output at the control's design-time(in IDE). It'll override the default
behavior of the control

#ControlDesigner.GetDesignTimeHtml Method
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemWebUIDesignControlDesignerClassGetDesignTimeHtmlTopic.asp


2. Apply the Custom Designer onto our CustomControl, just use the
"Designer" Attribute, such as:

[DefaultProperty("Text"),
ToolboxData("<{0}:TestControl runat=server></{0}:TestControl>"),
Designer(typeof(TestControlDesigner))]
public class TestControl : System.Web.UI.Control
{

In addition, here is a good tech article which has a complete tutorial on
developing a custom webcontrol and add rich design-time support for it.
(such as Control Designer, IDE intellisense ...)

#Adding Design-Time Support to ASP.NET Controls
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
/ASPNet-AddDesignTimeSupport.asp


Hope helps. Thanks

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(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
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top