Avatar of obb-taurus
obb-taurus
Flag for Canada asked on

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
C#ASP.NET

Avatar of undefined
Last Comment
esolve

8/22/2022 - Mon
Haver Ramirez

yes, it's possible...that's all what you need?
obb-taurus

ASKER
Given that I was not all to successful in figuring this out, I would appreciate if you could tell me how.

Thanks
Haver Ramirez

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
obb-taurus

ASKER
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
Haver Ramirez

UserControl1 x = new UserControl1(ParameterA,ParameterB,ParameterC)

Open in new window

can you post your code, or explain more clearly
obb-taurus

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
esolve

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.