Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-12-27
7
Medium Priority
?
465 Views
Last Modified: 2013-01-17
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
Comment
Question by:obb-taurus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:Crashman
ID: 38723643
yes, it's possible...that's all what you need?
0
 

Author Comment

by:obb-taurus
ID: 38724486
Given that I was not all to successful in figuring this out, I would appreciate if you could tell me how.

Thanks
0
 
LVL 8

Expert Comment

by:Crashman
ID: 38724519
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:obb-taurus
ID: 38724565
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
 
LVL 8

Expert Comment

by:Crashman
ID: 38724587
UserControl1 x = new UserControl1(ParameterA,ParameterB,ParameterC)

Open in new window

can you post your code, or explain more clearly
0
 

Author Comment

by:obb-taurus
ID: 38724861
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
 
LVL 6

Accepted Solution

by:
esolve earned 2000 total points
ID: 38786212
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question