R – Passing Parameters to New Sub of a User Control

asp.netconstructorparametersuser-controls

In ASP.Net, is it possible to pass parameters to the "New" constructor of a User Control class? In VB.Net:

Public Sub New(ByVal pRequiredParam As String)
  'Do something with required Param
End Sub

When I use this user control in a generic ASP.Net page, it doesn't prompt me for "pRequiredParam". Of course, if this was a "normal" class, I would have to supply "pRequiredParam" when I instantiate the object. Is this not possible with a User Control?

Best Answer

While you can certainly create a custom constructor like the one you show in your code sample, you cannot force the designer to make use of it. How would it determine what to send as argument to the constructor?

However, you can load the control dynamically in your code-behind file, and then use whatever parameters it defines. Note though that if you want the control to also work well with the graphical designer, I think that the control need to have a public constructor that takes no parameters (but I think you get that automatically in VB.NET?)