Create a CustomControl ... some doubt

A

Andrea

I'm trying to create a custom control, but I've some doubt.

I do the following to setup the designer default value

internal class MyControlDesigner : System.Web.UI.Design.ControlDesigner
{
public override void Initialize (IComponent component)
{
if (!base.ViewControlCreated)
{
base.Initialize(component);

MyControl xyz = (MyControl)component;
xyz.Test= "hello baby";
}

and added the [Designer(typeof(MyControlDesigner))] to my class that is
inheriting from WebControl.

When the control is placed on the form for the first time, the default value
are getted as expected.

What I'm not able to understand is why, when I run for the first time the
web page, my control properties display properties value as uninitialized
(null
or 0 according to the Variable type).

If I can see the value in the designer, those value should be theorically get
from the property's get method, so why at run-time the internal properties
will be resetted at run time?

Thanks
Andrea
 
S

Stoitcho Goutsev \(100\)

Andrea,

The designer is created by VS visual designer and outside VS (at run time)
no object of MyControlDesigner is created and no Initialize method is
called. That's why you get your Test property uninitiazed.

Don't use the designer to initialize your control. Do all necessary
initialization in the control code itself. The designer is to provide design
time experiance and doesn't exisit at run time.
 
A

Andrea

Don't use the designer to initialize your control. Do all necessary
initialization in the control code itself. The designer is to provide
design time experiance and doesn't exisit at run time.

But isn't there to get those value as default for the OnInit of the webcontrol?

Those value, in the default, is what user generally want to use, so I don't
expect they should be changed at all.

Bye
Andre
 
S

Stoitcho Goutsev \(100\)

Andrea,

Intialize the default values in the control constructor. Why do you want to
set them in the designer?
 

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

Top