Re: LoadControl() and Constructor Parameters
From: Hasani Blackwell (hblackwell_at_n0sp4m.popstick.com)
Date: 02/11/05
- Next message: fernandez.dan_at_gmail.com: "aspnet_wp will not release file handle"
- Previous message: Fabio: "VS .NET 2003 and .NET framework 2.0"
- In reply to: Sam Kuehn: "Re: LoadControl() and Constructor Parameters"
- Next in thread: Sam Kuehn: "Re: LoadControl() and Constructor Parameters"
- Reply: Sam Kuehn: "Re: LoadControl() and Constructor Parameters"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 11 Feb 2005 15:30:27 -0500
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.
"Sam Kuehn" <samkuehn@hotmail.com> wrote in message
news:123761632437123005255256@msnews.microsoft.com...
>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);
> }
>
>> 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.
>>
>> "Sam Kuehn" <samkuehn@hotmail.com> wrote in message
>> news:122879632436423422339114@msnews.microsoft.com...
>>
>>> 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.
>>>
>
>
>
- Next message: fernandez.dan_at_gmail.com: "aspnet_wp will not release file handle"
- Previous message: Fabio: "VS .NET 2003 and .NET framework 2.0"
- In reply to: Sam Kuehn: "Re: LoadControl() and Constructor Parameters"
- Next in thread: Sam Kuehn: "Re: LoadControl() and Constructor Parameters"
- Reply: Sam Kuehn: "Re: LoadControl() and Constructor Parameters"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|