PC Review


Reply
Thread Tools Rate Thread

Accessing Dynamically Loaded UserControls

 
 
=?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?=
Guest
Posts: n/a
 
      5th Feb 2007
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?

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      5th Feb 2007
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?
>

 
Reply With Quote
 
=?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?=
Guest
Posts: n/a
 
      5th Feb 2007
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

 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      5th Feb 2007
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

 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      5th Feb 2007
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?
>

 
Reply With Quote
 
=?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?=
Guest
Posts: n/a
 
      5th Feb 2007
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

 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      5th Feb 2007
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

 
Reply With Quote
 
=?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?=
Guest
Posts: n/a
 
      6th Feb 2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing Members of Dynamically Loaded Assemblies Jordan S. Microsoft C# .NET 1 19th Apr 2008 02:46 AM
Accessing the page from dynamically loaded control Alexey Smirnov Microsoft ASP .NET 1 27th Mar 2007 01:17 PM
Accessing properties when control loaded dynamically Vivek Sharma Microsoft ASP .NET 2 31st Oct 2005 12:25 AM
Accessing methods and properties of dynamically loaded controls Jeff Smith Microsoft ASP .NET 1 11th Oct 2004 02:49 PM
Events don't fire for dynamically loaded control when loaded by an event procedure RND Microsoft ASP .NET 1 16th Mar 2004 01:26 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:20 PM.