LoadControl() and Constructor Parameters

S

Sam Kuehn

How do I accomplish the fallowing (is it even possible). Say I write a UserControl
"MyControl.ascx". Now I use LoadControl("MyControl.ascx"). But I really
want MyControl to require parameters in the constructor for example MyContorl
oMyControl = new MyContorl(employeeid). However I need to load the control
at runtime so the have to call it this way LoadControl("MyControl.ascx")
and I get an error that I have not supplied any parameter to the constructor.
Is there anyway around this? I guess I could change the control so that
I can insatiate it without supplying parameters and assigning the properties
later; but that doesn't seem like a very clean solution. Thanks in advance.
 
S

Scott Allen

Controls have to have a paramerter-less constructor. You could use a
factory type pattern to keep a clean design. Perhaps even a static
method on the class like so:

MyUserControl uc = MyUserControl.Create(param1, param2) { ... }


What do you think?
 
S

Sam Kuehn

Hello Scott,
That would work great if I didn't have to load the control at runtime. See
I don't know what UserContol I am going to add at design time. It is dependant
on user input at runtime. Therefore, I cannot call a method (or throw parameters
at the constructor for that matter). Also, are you saying that I couldn't
do something like:

public class MyControl: System.Web.UI.UserControl
{
//Constructor
MyControll(int employeeid){.....}
}
Casue it seem that would work just fine.
 
S

Scott Allen

Hi Sam:

See my notes inline.

Hello Scott,
That would work great if I didn't have to load the control at runtime. See
I don't know what UserContol I am going to add at design time. It is dependant
on user input at runtime. Therefore, I cannot call a method (or throw parameters
at the constructor for that matter).

That's perfect for the factory pattern - it encapsulates all of the
logic needed to create and initialize an object, it also hides the
Type of the object being created. Some more details here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/factopattern.asp
Also, are you saying that I couldn't
do something like:

public class MyControl: System.Web.UI.UserControl
{
//Constructor
MyControll(int employeeid){.....}
}
Casue it seem that would work just fine.

Unfortunately not. When the runtime creates an instance of the control
it does so with the paramerter-less ctor.
 
S

Sam Kuehn

Thanks! This is exactly the type of info I was looking for. I will try
and implment a solution using the factory pattern.
 
H

Hasani Blackwell

All ascxs have only 1 constructor. Maybe the class MyControl.ascx inherits
from has overloaded constructors, but the asp.net runtime will call the
default constructor. You're going to need to take a different approach. If
you want the control to know the employeeid, use the session or something.
 
S

Sam Kuehn

I didn't want to call any Session stuff in my control. I was hoping not
to have to couple the control to this specific app that much. I would rather
pass the value in. Although the value I pass in will probably come form
the session object. Also all of the control in question do inherit from
a base control. Just for further claification here is the full picure of
what I am trying to do.

//The Base Control
namespace ExpenseReimbursment.GUI.Controls.Add
{
public delegate void ItemAddedEventHandler(object sender, EventArgs ea);
public class BaseControl : System.Web.UI.UserControl
{
// ToDo: Create constructor with params
// public BaseControl(SubCategoryEntity Subcategory)
// {
// _Subcategory = Subcategory;
// }
// private SubCategoryEntity _Subcategory;
// public SubCategoryEntity Subcategory
// {
// get
// {
// return _Subcategory;
// }
// set
// {
// _Subcategory = value;
// }
// }
#region Public Events
public event ItemAddedEventHandler ItemAdded;
protected virtual void OnItemAdded(EventArgs ea)
{
if (ItemAdded != null)
ItemAdded(this, ea);
}
#endregion

#region Public Methods
public void AddItem(ExpenseReportDetailEntity lineItem)
{
lineItem.Save();
OnItemAdded(new EventArgs());
}
public void AddItem(ExpenseReportDetailCollection lineItems)
{
lineItems.SaveMulti();
OnItemAdded(new EventArgs());
}
#endregion

}

}

The actual control will depend on the type of expense they are adding for
example:

public class DefaultControl : BaseControl {} //the default control.

And is called form the "Master Page" like this:
private void LoadAddItemsControl(int Subcategoryid)
{
phAdd.Controls.Clear();
SubCategoryEntity ojbSubCategoryEntity = new SubCategoryEntity(Subcategoryid);
string control = "~/Controls/Add/Default.ascx";
if (ojbSubCategoryEntity.Inputcontrol.ToString() != "")
control = "~/Controls/Add/" + ojbSubCategoryEntity.Inputcontrol;
GUI.Controls.Add.BaseControl ctlAddDetail = (GUI.Controls.Add.BaseControl)LoadControl(control);
ctlAddDetail.ID = "AddItem";
//Add Event Handler
ctlAddDetail.ItemAdded += new GUI.Controls.Add.ItemAddedEventHandler(this.ItemAdded);
//Add control
phAdd.Controls.Add(ctlAddDetail);
}
 
H

Hasani Blackwell

I think you're going to need a virtual Init[ializer] method and use that as
your 'constructor'. Though, to keep it flexible. I would make the method
signature
void Init[ializer](params object[] parameters). Just note that interfaces
are a collection virtual methods so you can do something like

puvlic Interface IBaseControl
{
void Initialize(params object[] parameters);
}

//then somehere in ur aspx code. but make sure this is done during the
Page_Init phase as because if you do this on say, Page_Load, the control
will execute all sequences to reach the same state as the caller (in this
case, Page_Load). It depends on what your intentions are and how the code
was written for the control though.

IBaseControl control = (IBaseControl) LoadControl("MyControl.ascx").
control.Initialize(employeeid);

or

public BaseControl
{
public virtual void Initialize(params object[] parameters);
}

//somewhere in aspx code.
BaseControl control = (BaseControl) LoadControl("MyControl.ascx").
control.Initialize(employeeid);


In the past, I never had a situation where a control or a page depended on a
non-default constructor to work. I've always used a system of, a well known
interface, or a class with virtual methods to expose methods (properties are
also methods) I needed to get the job done. And I disable session state and
other modules whenever I start a web project, I enable it as\when I need it
=]

And also, looking at your sample? code, you have the subcategory set in the
constructor, yet it does nothing, and you expose a get/set property that can
change the same field Why even have it in the constructor?!?!?. You should
also make the _Subcategory a public field and rename it to Subcategory,
because it would be no different (other than the lines of code saved) than
adding the get/set property that modify _Subcategory.
 
S

Sam Kuehn

I was hoping that I could require that a subcategory be set when the BaseControl
was instantiated (to help prevent errors and keep code clear). Right now
it is commented out for the reasons that I listed below (I get an error when
trying to instantiate a control based on this base control cause LoadControl()
was not passing parameters, subcategoryid, to the constructor). As far as
the getters and setters go: CodeRush creates them automatically and I am
just used to seeing things that way so I don't worry about changing it.
I guess if later on (will never happen) I could modify the private members
from within the class (again, I know in this situation it will never happen
but the code is already there so what the heck). Also, at some point, I
heard you should never have public variables, although the reason was never
really clear.
I think you're going to need a virtual Init[ializer] method and use
that as
your 'constructor'. Though, to keep it flexible. I would make the
method
signature
void Init[ializer](params object[] parameters). Just note that
interfaces
are a collection virtual methods so you can do something like
puvlic Interface IBaseControl
{
void Initialize(params object[] parameters);
}
//then somehere in ur aspx code. but make sure this is done during the
Page_Init phase as because if you do this on say, Page_Load, the
control will execute all sequences to reach the same state as the
caller (in this case, Page_Load). It depends on what your intentions
are and how the code was written for the control though.

IBaseControl control = (IBaseControl) LoadControl("MyControl.ascx").
control.Initialize(employeeid);

or

public BaseControl
{
public virtual void Initialize(params object[] parameters);
}
//somewhere in aspx code.
BaseControl control = (BaseControl) LoadControl("MyControl.ascx").
control.Initialize(employeeid);
In the past, I never had a situation where a control or a page
depended on a non-default constructor to work. I've always used a
system of, a well known interface, or a class with virtual methods to
expose methods (properties are also methods) I needed to get the job
done. And I disable session state and other modules whenever I start a
web project, I enable it as\when I need it =]

And also, looking at your sample? code, you have the subcategory set
in the constructor, yet it does nothing, and you expose a get/set
property that can change the same field Why even have it in the
constructor?!?!?. You should also make the _Subcategory a public field
and rename it to Subcategory, because it would be no different (other
than the lines of code saved) than adding the get/set property that
modify _Subcategory.

I didn't want to call any Session stuff in my control. I was hoping
not to have to couple the control to this specific app that much. I
would rather pass the value in. Although the value I pass in will
probably come form the session object. Also all of the control in
question do inherit from a base control. Just for further
claification here is the full picure of what I am trying to do.

//The Base Control
namespace ExpenseReimbursment.GUI.Controls.Add
{
public delegate void ItemAddedEventHandler(object sender, EventArgs
ea);
public class BaseControl : System.Web.UI.UserControl
{
// ToDo: Create constructor with params
// public BaseControl(SubCategoryEntity Subcategory)
// {
// _Subcategory = Subcategory;
// }
// private SubCategoryEntity _Subcategory;
// public SubCategoryEntity Subcategory
// {
// get
// {
// return _Subcategory;
// }
// set
// {
// _Subcategory = value;
// }
// }
#region Public Events
public event ItemAddedEventHandler ItemAdded;
protected virtual void OnItemAdded(EventArgs ea)
{
if (ItemAdded != null)
ItemAdded(this, ea);
}
#endregion
#region Public Methods
public void AddItem(ExpenseReportDetailEntity lineItem)
{
lineItem.Save();
OnItemAdded(new EventArgs());
}
public void AddItem(ExpenseReportDetailCollection lineItems)
{
lineItems.SaveMulti();
OnItemAdded(new EventArgs());
}
#endregion
}

}

The actual control will depend on the type of expense they are adding
for example:

public class DefaultControl : BaseControl {} //the default control.

And is called form the "Master Page" like this:
private void LoadAddItemsControl(int Subcategoryid)
{
phAdd.Controls.Clear();
SubCategoryEntity ojbSubCategoryEntity = new
SubCategoryEntity(Subcategoryid);
string control = "~/Controls/Add/Default.ascx";
if (ojbSubCategoryEntity.Inputcontrol.ToString() != "")
control = "~/Controls/Add/" + ojbSubCategoryEntity.Inputcontrol;
GUI.Controls.Add.BaseControl ctlAddDetail =
(GUI.Controls.Add.BaseControl)LoadControl(control);
ctlAddDetail.ID = "AddItem";
//Add Event Handler
ctlAddDetail.ItemAdded += new
GUI.Controls.Add.ItemAddedEventHandler(this.ItemAdded);
//Add control
phAdd.Controls.Add(ctlAddDetail);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top