• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

How to overload the constructor of a user control in ASP.NET

I've declared some objects on my ASP.NET web form that I would like to pass as parameters to the constructor of a user control so I can keep keep a reference to them on the main form.  Is it possible to overload the constructor of the user control for this purpose?

Thanks
0
obb-taurus
Asked:
obb-taurus
  • 3
  • 3
1 Solution
 
CrashmanCommented:
yes, it's possible...that's all what you need?
0
 
obb-taurusAuthor Commented:
Given that I was not all to successful in figuring this out, I would appreciate if you could tell me how.

Thanks
0
 
CrashmanCommented:
Overloading

public partial class UserControl1 : UserControl
    {
        //this is the contructor, without parameters;
        public UserControl1()
        {
            InitializeComponent();
        }

        public UserControl1(int field)
        {
            InitializeComponent();
            //do something with your field
        }

        public UserControl1(string field)
        {
            InitializeComponent();
            //do something with your field
        }
    }

Open in new window

all the anteriors are overloadings, constructor without parameter, constructor with one integer parameter, constructor with string parameter...all are valid because you can use overloading with variations in amount of parameters and tipe of them
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
obb-taurusAuthor Commented:
Sorry, I should have been more clear as to where I was experincing difficulty.  On the web form that is supposed to contain the user control, how do you instatiate the control and pass the parameters?

Thanks
0
 
CrashmanCommented:
UserControl1 x = new UserControl1(ParameterA,ParameterB,ParameterC)

Open in new window

can you post your code, or explain more clearly
0
 
obb-taurusAuthor Commented:
I'm a little confused by your response, unless I'm missing something, you can't instantiate a user control on a web form since it's added to the form as a tag in HTML.
0
 
esolveCommented:
What he means is that you can add a eg. literalcontrol to the page and then load the usercontrol like this:

<asp:PlaceHolder ID="phReportControl" runat="server"></asp:PlaceHolder>

 string sReportURL = "~/Modules/UserControlSample.ascx";
        Control toAdd = LoadControl(ResolveUrl(sReportURL));
        phReportControl.Controls.Add(toAdd);

If you have accessible properties on the control you can also set them here before loading the control.

There's no need really to use contructors since you're working in a web environment not windows.

Hope it helps!
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now