Professional Web Applications Themes

Loading controls dynamically + passing parameters - ASP.NET Building Controls

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...

  1. #1

    Default Loading controls dynamically + passing parameters

    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 Guest

  2. #2

    Default Re: Loading controls dynamically + passing parameters

    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" <jills_graham.au> wrote in message
    news:O8iDlqOMEHA.1900TK2MSFTNGP10.phx.gbl...
    > 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 Guest

  3. #3

    Default Re: Loading controls dynamically + passing parameters

    Mark's answer is spot on. Jill, you might find it helpful to read my
    article on User Controls, it talks about adding properties to user
    controls and a bevy of other user control-related topics.

    An Extensive Examination of User Controls
    [url]http://msdn.microsoft.com/asp.net/using/building/webcontrols/default.aspx?pull=/library/en-us/dnaspp/html/usercontrols.asp[/url]

    Happy Programming!

    --

    Scott Mitchell
    [email]mitchell4guysfromrolla.com[/email]
    [url]http://www.4GuysFromRolla.com[/url]
    [url]http://www.ASPFAQs.com[/url]
    [url]http://www.ASPMessageboard.com[/url]

    * When you think ASP, think 4GuysFromRolla.com!
    Scott Mitchell [MVP] Guest

Similar Threads

  1. Dynamically loading Tree View controls
    By Subramanian Ramachandran via .NET 247 in forum ASP.NET Building Controls
    Replies: 0
    Last Post: May 17th, 08:20 AM
  2. Replies: 1
    Last Post: May 26th, 06:41 AM
  3. Passing parameters to Windows Controls
    By Neelima Godugu in forum ASP.NET Building Controls
    Replies: 0
    Last Post: December 22nd, 03:50 PM
  4. passing parameters to server behaviors dynamically.
    By ramya.subramanya webforumsuser@macromedia.com in forum Macromedia Dreamweaver
    Replies: 0
    Last Post: July 17th, 07:00 AM
  5. Passing parameters to user controls
    By Akira in forum ASP.NET General
    Replies: 2
    Last Post: July 2nd, 06:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139