views:

2107

answers:

2

Is there a way to pass the parameters to the LoadControl function when loading the user control dynamically?

+1  A: 

All you need to do is create a descendant of the UserControl class, add a default constructor and another constructor that takes your parameters. The parameterless constructor is necessary for designer support.

public class MyControl : UserControl
{
    public MyControl() : base()
    {
       // do initialization stuff...
    }

    public MyControl(int parameter) : this()
    {
       // do additional stuff with parameter
    }
}
Thorsten Dittmar
Thanks, yes, I know how to write a constructor:) I was more referring to the dynamic User Control creation with LoadControl function. How do I pass a parameter to the constructor to this function. Sorry, if I weren't clear.
gnomixa
I thought the problem was with the Visual Studio designer not displaying the user control (which may be the case when you do not provide a parameterless constructor)...
Thorsten Dittmar
A: 

I found a solution that uses reflection here

gnomixa