Loading controls dynamically + passing parameters

Discussion in 'ASP .Net Building Controls' started by Jill Graham, May 3, 2004.

  1. Jill Graham

    Jill Graham Guest

    Hi,

    I can use following syntax to add user controls directly to my pages :
    <XYZ:myControl parameter1="value1" parameter2="value2" runat="server" />
    The control "myControl" is executed using the supplied parameters parameter1
    and parameter2

    Now my question :
    I've found two ways to add controls dynamically to my page :

    method 1
    ---------
    page.loadControl("<.ascx file>")

    method 2
    ---------
    activator.createInstance(type.getType("<class name>", true))

    The problem is : how can I pass the parameters parameter1 and parameter2 to
    the control ?


    Thanks for any advice
    Jill
    Jill Graham, May 3, 2004
    #1
    1. Advertising

  2. The Page.LoadControl method will actually return a reference to the control
    such as
    MyControl myControl1 = (MyControl)Page.LoadControl("mycontrol.ascs");

    You now have a reference to the control. You can now set any properties that
    you have on the control such as
    myControl1.MyParameter = "Something";

    To access this value though, you need to be careful where you work with it.
    If have code in the control's Page_Load event, override the OnPreRender
    event and move the code there instead. The Page_Load event of the control
    happens before the Page_Load event of the parent page so you have to use a
    later event for the control.

    Hope this helps,
    Mark Fitzpatrick
    Microsoft MVP - FrontPage


    "Jill Graham" <> wrote in message
    news:...
    > Hi,
    >
    > I can use following syntax to add user controls directly to my pages :
    > <XYZ:myControl parameter1="value1" parameter2="value2" runat="server" />
    > The control "myControl" is executed using the supplied parameters

    parameter1
    > and parameter2
    >
    > Now my question :
    > I've found two ways to add controls dynamically to my page :
    >
    > method 1
    > ---------
    > page.loadControl("<.ascx file>")
    >
    > method 2
    > ---------
    > activator.createInstance(type.getType("<class name>", true))
    >
    > The problem is : how can I pass the parameters parameter1 and parameter2

    to
    > the control ?
    >
    >
    > Thanks for any advice
    > Jill
    >
    >
    Mark Fitzpatrick, May 3, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jill Graham
    Replies:
    1
    Views:
    335
    Jim Corey
    May 3, 2004
  2. Josh
    Replies:
    1
    Views:
    9,511
    Jeffrey Palermo [MCP]
    May 26, 2004
  3. Replies:
    3
    Views:
    4,026
  4. JB McMichael
    Replies:
    0
    Views:
    374
    JB McMichael
    Jul 26, 2005
  5. Jamil
    Replies:
    1
    Views:
    2,365
    =?Utf-8?B?SFA=?=
    Nov 24, 2005
Loading...

Share This Page