1

Hello i have a Usercontrol with Constructor

public Person(String Id, String Name)

i want to load this UserControl as isntance like:

Control tmpControl = (Person)LoadControl(typeof(Person), new object[] { Id, Name});

the instance of the .cs work but the usercontrol controls are all null.

if i do:

Control tmpControl = (Person)LoadControl("Person.ascx");

i have controls in the instance but he use the default empty construcor.

How to get booth the correct controls and class instance ???

One other try is:

Control tmpControll = (new Person(Id, Name)).LoadControl("Person.ascx");

this work as follows: (new Person(Id, Name)) = new class inctance and .LoadControl("Person.ascx") makes again a clompete new instance that means the first line do the right instance but is overwritten by the secound part

I dont want the example:

Control c = Page.LoadControl("/UserControl/webMenu.ascx");
webMenu a = (webMenu)c;
a.Title = "This is a Title";

this is not really what i want !!!!!!!!!!!

8
  • possible duplicate of Load user control dynamically with parameters Commented Oct 30, 2014 at 19:55
  • I can't think of any instances where controls have different parameters in their constructors. Normally you declaratively set the properties of the controls in markup, or manually set them one by one if the control is dynamically created. But if you really want to load them dynamically and pass them in the constructor why not just do something like Person tmpControl = new Person(Id, Name);? Commented Oct 30, 2014 at 19:57
  • I want to add the Controls of the .ascx into the Page, but i want to call a specific controctur that the controls from the ascx cointains the data i want. Commented Oct 30, 2014 at 20:09
  • So create a control like I showed you in my previous comment, then add it to the Controls collection of the parent you want it to have. Probably advisable to do this early enough in the page lifecycle (such as Page_Init) that the control gets persisted across postbacks. Commented Oct 30, 2014 at 20:16
  • That dosent work and it is the 3rd of my example called "other try is" Commented Oct 30, 2014 at 20:21

1 Answer 1

1

I think the LoadControl with the overload (Type t, object[] parameters) is a big fail and makes no sense

One Solution is to use an additional 3rd overload like: http://www.grumpydev.com/2009/01/05/passing-parameters-using-loadcontrol/

Or use this solution:

Control c = Page.LoadControl("/UserControl/webMenu.ascx");
webMenu a = (webMenu)c;
a.Title = "This is a Title";

And verify at Page_Load that the class variables are set, than call specific functions like the Constructor.

I hope Microsoft implement a 3rd overload for LoadControl that work. I mean LoadControl(strin path) call the deafault empty constructor, why there is no option to call a specific constructor???

I tried thinks like:

TemplateControl tmpControll = (Person)LoadControl("Person.ascx");
UserControl tmpControll2 = (Person)LoadControl(typeof(Person), new object[]{ID,Name});
tmpControll.Page.Controls.Add(tmpControll2);
//this not work correctly !!

that means make a instance and add the UserControls additional, that they are not null at panel.Controls.Add(tmpControll); But at Controls.Add he perform the Page_load twice, one time with using the right Constructor and UserControlls NULL and the Secound Page_load use default Constructor with existings UserControll.

The aim is to make a instance with the constructor i want and add the UserControls to this instance and when i call Controls.Add he use both things together and not one after another.

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

Comments

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.