Accessing Dynamically Loaded UserControls

Discussion in 'ASP .Net' started by =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?=, Feb 5, 2007.

  1. I have a problem with accessing controls that I have loaded dynamically and
    added to a web page.

    The scenario:
    I have a webpage that displays multiple instances of a user control on the
    page. The number of controls that are displayed can be adjusted by the user
    so the controls are added to the page dynamically using the following code in
    the Page_Init event handler:

    For i as Integer = 1 to NumberOfControls
    Dim ctl as MyCustomControl = CType(LoadControl(“MyCustomControl.ascxâ€),
    MyCustomControl)

    ' set some custom properties on ctl
    panControls.Controls.Add(ctl)
    Next

    This part of the code all works fine and I can load the controls onto the
    page without any problems.

    The problem:
    MyCustomControl contains a save method which will update the database with
    the data that is entered onto the control. Rather than having a save button
    on the control which fires the update, I have one save button on the page
    which is meant to update all controls on the page. I intended to loop
    through the Controls collection of panControls when the save button is
    clicked and call save on each of the custom controls that are in the
    collection. It seems I am unable to do this however. When I check
    panControls.Controls.Count it only returns 1 regardless of how many custom
    controls I have added (the control it is counting by the way is a literal
    control which seems to be put there automatically). I have stepped through
    the code that is loading the controls and the count property is being updated
    to reflect that I am adding controls, however they aren’t there when the page
    is posted back (I’ve also ensured the code to load the controls is only
    executed if Page.IsPostBack is false).

    Can anybody suggest where im going wrong?
     
    =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?=, Feb 5, 2007
    #1
    1. Advertising

  2. Are you giving each control a unique ID propertry before you add it to the
    Panel?
    Peter

    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "..:: Kevin ::.." wrote:

    > I have a problem with accessing controls that I have loaded dynamically and
    > added to a web page.
    >
    > The scenario:
    > I have a webpage that displays multiple instances of a user control on the
    > page. The number of controls that are displayed can be adjusted by the user
    > so the controls are added to the page dynamically using the following code in
    > the Page_Init event handler:
    >
    > For i as Integer = 1 to NumberOfControls
    > Dim ctl as MyCustomControl = CType(LoadControl(“MyCustomControl.ascxâ€),
    > MyCustomControl)
    >
    > ' set some custom properties on ctl
    > panControls.Controls.Add(ctl)
    > Next
    >
    > This part of the code all works fine and I can load the controls onto the
    > page without any problems.
    >
    > The problem:
    > MyCustomControl contains a save method which will update the database with
    > the data that is entered onto the control. Rather than having a save button
    > on the control which fires the update, I have one save button on the page
    > which is meant to update all controls on the page. I intended to loop
    > through the Controls collection of panControls when the save button is
    > clicked and call save on each of the custom controls that are in the
    > collection. It seems I am unable to do this however. When I check
    > panControls.Controls.Count it only returns 1 regardless of how many custom
    > controls I have added (the control it is counting by the way is a literal
    > control which seems to be put there automatically). I have stepped through
    > the code that is loading the controls and the count property is being updated
    > to reflect that I am adding controls, however they aren’t there when the page
    > is posted back (I’ve also ensured the code to load the controls is only
    > executed if Page.IsPostBack is false).
    >
    > Can anybody suggest where im going wrong?
    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Feb 5, 2007
    #2
    1. Advertising

  3. Peter,

    I have tried giving the controls a unique ID and then trying to pick up the
    control using panControls.FindControl("ControlID") but this returns nothing.

    Kevin

    "Peter Bromberg [C# MVP]" wrote:

    > Are you giving each control a unique ID propertry before you add it to the
    > Panel?
    > Peter
    >
    > --
    > Site: http://www.eggheadcafe.com
    > UnBlog: http://petesbloggerama.blogspot.com
    > Short urls & more: http://ittyurl.net
     
    =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?=, Feb 5, 2007
    #3
  4. Any controls that are added programmatically to a page (or a container
    control on the page) need to be re-created when the page reloads or on a
    postback. They don't just "stick" once you put them there. Could this be your
    issue?
    Peter

    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "..:: Kevin ::.." wrote:

    > Peter,
    >
    > I have tried giving the controls a unique ID and then trying to pick up the
    > control using panControls.FindControl("ControlID") but this returns nothing.
    >
    > Kevin
    >
    > "Peter Bromberg [C# MVP]" wrote:
    >
    > > Are you giving each control a unique ID propertry before you add it to the
    > > Panel?
    > > Peter
    > >
    > > --
    > > Site: http://www.eggheadcafe.com
    > > UnBlog: http://petesbloggerama.blogspot.com
    > > Short urls & more: http://ittyurl.net
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Feb 5, 2007
    #4
  5. =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?=

    bruce barker Guest

    you need to recreate the controls on postback so they exist. if its just
    a count, store the number in a hidden field (or viewstate), you can look
    at on postback.

    -- bruce (sqlwork.com)


    ...:: Kevin ::.. wrote:
    > I have a problem with accessing controls that I have loaded dynamically and
    > added to a web page.
    >
    > The scenario:
    > I have a webpage that displays multiple instances of a user control on the
    > page. The number of controls that are displayed can be adjusted by the user
    > so the controls are added to the page dynamically using the following code in
    > the Page_Init event handler:
    >
    > For i as Integer = 1 to NumberOfControls
    > Dim ctl as MyCustomControl = CType(LoadControl(“MyCustomControl.ascxâ€),
    > MyCustomControl)
    >
    > ' set some custom properties on ctl
    > panControls.Controls.Add(ctl)
    > Next
    >
    > This part of the code all works fine and I can load the controls onto the
    > page without any problems.
    >
    > The problem:
    > MyCustomControl contains a save method which will update the database with
    > the data that is entered onto the control. Rather than having a save button
    > on the control which fires the update, I have one save button on the page
    > which is meant to update all controls on the page. I intended to loop
    > through the Controls collection of panControls when the save button is
    > clicked and call save on each of the custom controls that are in the
    > collection. It seems I am unable to do this however. When I check
    > panControls.Controls.Count it only returns 1 regardless of how many custom
    > controls I have added (the control it is counting by the way is a literal
    > control which seems to be put there automatically). I have stepped through
    > the code that is loading the controls and the count property is being updated
    > to reflect that I am adding controls, however they aren’t there when the page
    > is posted back (I’ve also ensured the code to load the controls is only
    > executed if Page.IsPostBack is false).
    >
    > Can anybody suggest where im going wrong?
    >
     
    bruce barker, Feb 5, 2007
    #5
  6. This sounds like it could be the cause of my problem. If I do need to
    re-create the controls on postback however, how do I get at the data that has
    been entered into the controls so that I can re-create them?

    I assumed with me creating a composite control using standard .NET controls
    that persistance of data would just come for free without me having to add
    any aditional code.

    Kevin

    "Peter Bromberg [C# MVP]" wrote:

    > Any controls that are added programmatically to a page (or a container
    > control on the page) need to be re-created when the page reloads or on a
    > postback. They don't just "stick" once you put them there. Could this be your
    > issue?
    > Peter
    >
    > --
    > Site: http://www.eggheadcafe.com
    > UnBlog: http://petesbloggerama.blogspot.com
    > Short urls & more: http://ittyurl.net
     
    =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?=, Feb 5, 2007
    #6
  7. If you recreate your controls exactly in the same order as originally, with
    the same ID's then ViewState will be able to "hook back up".

    For things that ViewState does not handle, you'll need to wire up some
    custom code.

    Peter

    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "..:: Kevin ::.." wrote:

    > This sounds like it could be the cause of my problem. If I do need to
    > re-create the controls on postback however, how do I get at the data that has
    > been entered into the controls so that I can re-create them?
    >
    > I assumed with me creating a composite control using standard .NET controls
    > that persistance of data would just come for free without me having to add
    > any aditional code.
    >
    > Kevin
    >
    > "Peter Bromberg [C# MVP]" wrote:
    >
    > > Any controls that are added programmatically to a page (or a container
    > > control on the page) need to be re-created when the page reloads or on a
    > > postback. They don't just "stick" once you put them there. Could this be your
    > > issue?
    > > Peter
    > >
    > > --
    > > Site: http://www.eggheadcafe.com
    > > UnBlog: http://petesbloggerama.blogspot.com
    > > Short urls & more: http://ittyurl.net
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Feb 5, 2007
    #7
  8. Ive tested this today and it works fine, this is what I was after.

    Thanks for your help!

    Kevin

    "Peter Bromberg [C# MVP]" wrote:

    > If you recreate your controls exactly in the same order as originally, with
    > the same ID's then ViewState will be able to "hook back up".
    >
    > For things that ViewState does not handle, you'll need to wire up some
    > custom code.
    >
    > Peter
    >
    > --
    > Site: http://www.eggheadcafe.com
    > UnBlog: http://petesbloggerama.blogspot.com
    > Short urls & more: http://ittyurl.net
     
    =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?=, Feb 6, 2007
    #8
    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. Jeff Smith
    Replies:
    1
    Views:
    526
    Martin Dechev
    Oct 11, 2004
  2. Vivek Sharma
    Replies:
    2
    Views:
    755
    Vivek Sharma
    Oct 31, 2005
  3. Alexey Smirnov
    Replies:
    1
    Views:
    310
    Alexey Smirnov
    Mar 27, 2007
  4. Sven Schimmel

    Caching of dynamic loaded usercontrols

    Sven Schimmel, Jul 31, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    122
    jan v
    Jul 31, 2003
  5. ..:: Kevin ::..

    Accessing Dynamically Loaded UserControls

    ..:: Kevin ::.., Feb 5, 2007, in forum: ASP .Net Web Controls
    Replies:
    7
    Views:
    157
    ..:: Kevin ::..
    Feb 6, 2007
Loading...

Share This Page