Re: User control constructor called twice

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi,

I'm so sorry - I accidentally gave you code overloading the wrong method :|

The following code works just fine:

public class CustomDataGridView : DataGridView
{
protected override void OnCreateControl()
{
if (Site == null || !Site.DesignMode)
{
Columns.Add("Test 1", "Header 1");
Columns.Add("Test 2", "Header 2");
}

base.OnCreateControl();
}
}

Yeah, I guess it does make sense that the constructor gets called at
design time, and then of course at run time. What I don't get is why
the constructor gets called twice for the same instance of that
control? Why doesn't the design time instance get thrown out and a new
one created at run time? How can you even call a constructor twice for
the same instance?

You can't call a constructor twice on the same instance. The runtime
instance and design-time instance are not the same. When you start your
application, even with a debugger attached, it will run in its own process.

<snip - sorry - >

--
Dave Sexton


.


Quantcast