3

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

2 Answers 2

3

I found a solution that uses reflection here

Sign up to request clarification or add additional context in comments.

Comments

1

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
    }
}

2 Comments

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.
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)...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.