LoadControl() and Constructor Parameters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sam Kuehn

    LoadControl() and Constructor Parameters

    How do I accomplish the fallowing (is it even possible). Say I write a UserControl
    "MyControl.ascx ". Now I use LoadControl("My Control.ascx"). But I really
    want MyControl to require parameters in the constructor for example MyContorl
    oMyControl = new MyContorl(emplo yeeid). However I need to load the control
    at runtime so the have to call it this way LoadControl("My Control.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.


  • Scott Allen

    #2
    Re: LoadControl() and Constructor Parameters

    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.C reate(param1, param2) { ... }


    What do you think?

    --
    Scott




    On Thu, 10 Feb 2005 13:25:41 -0800, Sam Kuehn <samkuehn@hotma il.com>
    wrote:
    [color=blue]
    >How do I accomplish the fallowing (is it even possible). Say I write a UserControl
    >"MyControl.asc x". Now I use LoadControl("My Control.ascx"). But I really
    >want MyControl to require parameters in the constructor for example MyContorl
    >oMyControl = new MyContorl(emplo yeeid). However I need to load the control
    >at runtime so the have to call it this way LoadControl("My Control.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.
    >[/color]

    Comment

    • Sam Kuehn

      #3
      Re: LoadControl() and Constructor Parameters

      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.U serControl
      {
      //Constructor
      MyControll(int employeeid){... ..}
      }
      Casue it seem that would work just fine.
      [color=blue]
      > 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.C reate(param1, param2) { ... }
      >
      > What do you think?
      >
      > --
      > Scott
      > http://www.OdeToCode.com/blogs/scott/
      > On Thu, 10 Feb 2005 13:25:41 -0800, Sam Kuehn <samkuehn@hotma il.com>
      > wrote:
      >[color=green]
      >> How do I accomplish the fallowing (is it even possible). Say I write
      >> a UserControl "MyControl.ascx ". Now I use
      >> LoadControl("My Control.ascx"). But I really want MyControl to
      >> require parameters in the constructor for example MyContorl
      >> oMyControl = new MyContorl(emplo yeeid). However I need to load the
      >> control at runtime so the have to call it this way
      >> LoadControl("My Control.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.
      >>[/color][/color]



      Comment

      • Scott Allen

        #4
        Re: LoadControl() and Constructor Parameters

        Hi Sam:

        See my notes inline.

        On Thu, 10 Feb 2005 14:14:30 -0800, Sam Kuehn <samkuehn@hotma il.com>
        wrote:
        [color=blue]
        >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).[/color]

        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:

        [color=blue]
        >Also, are you saying that I couldn't
        >do something like:
        >
        >public class MyControl: System.Web.UI.U serControl
        >{
        > //Constructor
        > MyControll(int employeeid){... ..}
        >}
        >Casue it seem that would work just fine.
        >[/color]

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

        --
        Scott

        [color=blue][color=green]
        >> 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.C reate(param1, param2) { ... }
        >>
        >> What do you think?
        >>
        >> --
        >> Scott
        >> http://www.OdeToCode.com/blogs/scott/
        >> On Thu, 10 Feb 2005 13:25:41 -0800, Sam Kuehn <samkuehn@hotma il.com>
        >> wrote:
        >>[color=darkred]
        >>> How do I accomplish the fallowing (is it even possible). Say I write
        >>> a UserControl "MyControl.ascx ". Now I use
        >>> LoadControl("My Control.ascx"). But I really want MyControl to
        >>> require parameters in the constructor for example MyContorl
        >>> oMyControl = new MyContorl(emplo yeeid). However I need to load the
        >>> control at runtime so the have to call it this way
        >>> LoadControl("My Control.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.
        >>>[/color][/color]
        >
        >[/color]

        Comment

        • Hasani Blackwell

          #5
          Re: LoadControl() and Constructor Parameters

          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@hotma il.com> wrote in message
          news:1228796324 36423422339114@ msnews.microsof t.com...[color=blue]
          > How do I accomplish the fallowing (is it even possible). Say I write a
          > UserControl "MyControl.ascx ". Now I use LoadControl("My Control.ascx").
          > But I really want MyControl to require parameters in the constructor for
          > example MyContorl oMyControl = new MyContorl(emplo yeeid). However I need
          > to load the control at runtime so the have to call it this way
          > LoadControl("My Control.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.
          >
          >[/color]


          Comment

          • Sam Kuehn

            #6
            Re: LoadControl() and Constructor Parameters

            Thanks! This is exactly the type of info I was looking for. I will try
            and implment a solution using the factory pattern.
            [color=blue]
            > Hi Sam:
            >
            > See my notes inline.
            >
            > On Thu, 10 Feb 2005 14:14:30 -0800, Sam Kuehn <samkuehn@hotma il.com>
            > wrote:
            >[color=green]
            >> 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).[/color]
            > 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/de...ry/en-us/dnbda
            > /html/factopattern.as p
            >[color=green]
            >> Also, are you saying that I couldn't do something like:
            >>
            >> public class MyControl: System.Web.UI.U serControl
            >> {
            >> //Constructor
            >> MyControll(int employeeid){... ..}
            >> }
            >> Casue it seem that would work just fine.[/color]
            > Unfortunately not. When the runtime creates an instance of the control
            > it does so with the paramerter-less ctor.
            >
            > --
            > Scott
            > http://www.OdeToCode.com/blogs/scott/[color=green][color=darkred]
            >>> 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.C reate(param1, param2) { ... }
            >>>
            >>> What do you think?
            >>>
            >>> --
            >>> Scott
            >>> http://www.OdeToCode.com/blogs/scott/
            >>> On Thu, 10 Feb 2005 13:25:41 -0800, Sam Kuehn <samkuehn@hotma il.com>
            >>> wrote:
            >>>> How do I accomplish the fallowing (is it even possible). Say I
            >>>> write a UserControl "MyControl.ascx ". Now I use
            >>>> LoadControl("My Control.ascx"). But I really want MyControl to
            >>>> require parameters in the constructor for example MyContorl
            >>>> oMyControl = new MyContorl(emplo yeeid). However I need to load the
            >>>> control at runtime so the have to call it this way
            >>>> LoadControl("My Control.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.
            >>>>[/color][/color][/color]



            Comment

            • Sam Kuehn

              #7
              Re: LoadControl() and Constructor Parameters

              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 ExpenseReimburs ment.GUI.Contro ls.Add
              {
              public delegate void ItemAddedEventH andler(object sender, EventArgs ea);
              public class BaseControl : System.Web.UI.U serControl
              {
              // ToDo: Create constructor with params
              // public BaseControl(Sub CategoryEntity Subcategory)
              // {
              // _Subcategory = Subcategory;
              // }
              // private SubCategoryEnti ty _Subcategory;
              // public SubCategoryEnti ty Subcategory
              // {
              // get
              // {
              // return _Subcategory;
              // }
              // set
              // {
              // _Subcategory = value;
              // }
              // }
              #region Public Events
              public event ItemAddedEventH andler ItemAdded;
              protected virtual void OnItemAdded(Eve ntArgs ea)
              {
              if (ItemAdded != null)
              ItemAdded(this, ea);
              }
              #endregion

              #region Public Methods
              public void AddItem(Expense ReportDetailEnt ity lineItem)
              {
              lineItem.Save() ;
              OnItemAdded(new EventArgs());
              }
              public void AddItem(Expense ReportDetailCol lection lineItems)
              {
              lineItems.SaveM ulti();
              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 LoadAddItemsCon trol(int Subcategoryid)
              {
              phAdd.Controls. Clear();
              SubCategoryEnti ty ojbSubCategoryE ntity = new SubCategoryEnti ty(Subcategoryi d);
              string control = "~/Controls/Add/Default.ascx";
              if (ojbSubCategory Entity.Inputcon trol.ToString() != "")
              control = "~/Controls/Add/" + ojbSubCategoryE ntity.Inputcont rol;
              GUI.Controls.Ad d.BaseControl ctlAddDetail = (GUI.Controls.A dd.BaseControl) LoadControl(con trol);
              ctlAddDetail.ID = "AddItem";
              //Add Event Handler
              ctlAddDetail.It emAdded += new GUI.Controls.Ad d.ItemAddedEven tHandler(this.I temAdded);
              //Add control
              phAdd.Controls. Add(ctlAddDetai l);
              }
              [color=blue]
              > 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@hotma il.com> wrote in message
              > news:1228796324 36423422339114@ msnews.microsof t.com...
              >[color=green]
              >> How do I accomplish the fallowing (is it even possible). Say I write
              >> a UserControl "MyControl.ascx ". Now I use
              >> LoadControl("My Control.ascx"). But I really want MyControl to require
              >> parameters in the constructor for example MyContorl oMyControl = new
              >> MyContorl(emplo yeeid). However I need to load the control at runtime
              >> so the have to call it this way LoadControl("My Control.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.
              >>[/color][/color]



              Comment

              • Hasani Blackwell

                #8
                Re: LoadControl() and Constructor Parameters

                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(para ms 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("My Control.ascx").
                control.Initial ize(employeeid) ;

                or

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

                //somewhere in aspx code.
                BaseControl control = (BaseControl) LoadControl("My Control.ascx").
                control.Initial ize(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@hotma il.com> wrote in message
                news:1237616324 37123005255256@ msnews.microsof t.com...[color=blue]
                >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 ExpenseReimburs ment.GUI.Contro ls.Add
                > {
                > public delegate void ItemAddedEventH andler(object sender, EventArgs ea);
                > public class BaseControl : System.Web.UI.U serControl
                > {
                > // ToDo: Create constructor with params
                > // public BaseControl(Sub CategoryEntity Subcategory)
                > // {
                > // _Subcategory = Subcategory;
                > // }
                > // private SubCategoryEnti ty _Subcategory;
                > // public SubCategoryEnti ty Subcategory
                > // {
                > // get
                > // {
                > // return _Subcategory;
                > // }
                > // set
                > // {
                > // _Subcategory = value;
                > // }
                > // }
                > #region Public Events
                > public event ItemAddedEventH andler ItemAdded;
                > protected virtual void OnItemAdded(Eve ntArgs ea)
                > {
                > if (ItemAdded != null)
                > ItemAdded(this, ea);
                > }
                > #endregion
                >
                > #region Public Methods
                > public void AddItem(Expense ReportDetailEnt ity lineItem)
                > {
                > lineItem.Save() ;
                > OnItemAdded(new EventArgs());
                > }
                > public void AddItem(Expense ReportDetailCol lection lineItems)
                > {
                > lineItems.SaveM ulti();
                > 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 LoadAddItemsCon trol(int Subcategoryid)
                > {
                > phAdd.Controls. Clear();
                > SubCategoryEnti ty ojbSubCategoryE ntity = new
                > SubCategoryEnti ty(Subcategoryi d);
                > string control = "~/Controls/Add/Default.ascx";
                > if (ojbSubCategory Entity.Inputcon trol.ToString() != "")
                > control = "~/Controls/Add/" + ojbSubCategoryE ntity.Inputcont rol;
                > GUI.Controls.Ad d.BaseControl ctlAddDetail =
                > (GUI.Controls.A dd.BaseControl) LoadControl(con trol);
                > ctlAddDetail.ID = "AddItem";
                > //Add Event Handler
                > ctlAddDetail.It emAdded += new
                > GUI.Controls.Ad d.ItemAddedEven tHandler(this.I temAdded);
                > //Add control
                > phAdd.Controls. Add(ctlAddDetai l);
                > }
                >[color=green]
                >> 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@hotma il.com> wrote in message
                >> news:1228796324 36423422339114@ msnews.microsof t.com...
                >>[color=darkred]
                >>> How do I accomplish the fallowing (is it even possible). Say I write
                >>> a UserControl "MyControl.ascx ". Now I use
                >>> LoadControl("My Control.ascx"). But I really want MyControl to require
                >>> parameters in the constructor for example MyContorl oMyControl = new
                >>> MyContorl(emplo yeeid). However I need to load the control at runtime
                >>> so the have to call it this way LoadControl("My Control.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.
                >>>[/color][/color]
                >
                >
                >[/color]


                Comment

                • Sam Kuehn

                  #9
                  Re: LoadControl() and Constructor Parameters

                  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.
                  [color=blue]
                  > 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(para ms 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("My Control.ascx").
                  > control.Initial ize(employeeid) ;
                  >
                  > or
                  >
                  > public BaseControl
                  > {
                  > public virtual void Initialize(para ms object[] parameters);
                  > }
                  > //somewhere in aspx code.
                  > BaseControl control = (BaseControl) LoadControl("My Control.ascx").
                  > control.Initial ize(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@hotma il.com> wrote in message
                  > news:1237616324 37123005255256@ msnews.microsof t.com...
                  >[color=green]
                  >> 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 ExpenseReimburs ment.GUI.Contro ls.Add
                  >> {
                  >> public delegate void ItemAddedEventH andler(object sender, EventArgs
                  >> ea);
                  >> public class BaseControl : System.Web.UI.U serControl
                  >> {
                  >> // ToDo: Create constructor with params
                  >> // public BaseControl(Sub CategoryEntity Subcategory)
                  >> // {
                  >> // _Subcategory = Subcategory;
                  >> // }
                  >> // private SubCategoryEnti ty _Subcategory;
                  >> // public SubCategoryEnti ty Subcategory
                  >> // {
                  >> // get
                  >> // {
                  >> // return _Subcategory;
                  >> // }
                  >> // set
                  >> // {
                  >> // _Subcategory = value;
                  >> // }
                  >> // }
                  >> #region Public Events
                  >> public event ItemAddedEventH andler ItemAdded;
                  >> protected virtual void OnItemAdded(Eve ntArgs ea)
                  >> {
                  >> if (ItemAdded != null)
                  >> ItemAdded(this, ea);
                  >> }
                  >> #endregion
                  >> #region Public Methods
                  >> public void AddItem(Expense ReportDetailEnt ity lineItem)
                  >> {
                  >> lineItem.Save() ;
                  >> OnItemAdded(new EventArgs());
                  >> }
                  >> public void AddItem(Expense ReportDetailCol lection lineItems)
                  >> {
                  >> lineItems.SaveM ulti();
                  >> 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 LoadAddItemsCon trol(int Subcategoryid)
                  >> {
                  >> phAdd.Controls. Clear();
                  >> SubCategoryEnti ty ojbSubCategoryE ntity = new
                  >> SubCategoryEnti ty(Subcategoryi d);
                  >> string control = "~/Controls/Add/Default.ascx";
                  >> if (ojbSubCategory Entity.Inputcon trol.ToString() != "")
                  >> control = "~/Controls/Add/" + ojbSubCategoryE ntity.Inputcont rol;
                  >> GUI.Controls.Ad d.BaseControl ctlAddDetail =
                  >> (GUI.Controls.A dd.BaseControl) LoadControl(con trol);
                  >> ctlAddDetail.ID = "AddItem";
                  >> //Add Event Handler
                  >> ctlAddDetail.It emAdded += new
                  >> GUI.Controls.Ad d.ItemAddedEven tHandler(this.I temAdded);
                  >> //Add control
                  >> phAdd.Controls. Add(ctlAddDetai l);
                  >> }[color=darkred]
                  >>> 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@hotma il.com> wrote in message
                  >>> news:1228796324 36423422339114@ msnews.microsof t.com...
                  >>>
                  >>>> How do I accomplish the fallowing (is it even possible). Say I
                  >>>> write a UserControl "MyControl.ascx ". Now I use
                  >>>> LoadControl("My Control.ascx"). But I really want MyControl to
                  >>>> require parameters in the constructor for example MyContorl
                  >>>> oMyControl = new MyContorl(emplo yeeid). However I need to load the
                  >>>> control at runtime so the have to call it this way
                  >>>> LoadControl("My Control.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.
                  >>>>[/color][/color][/color]



                  Comment

                  Working...