473,834 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing Dynamically Loaded UserControls

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 NumberOfControl s
Dim ctl as MyCustomControl = CType(LoadContr ol(“MyCustomC ontrol.ascx”) ,
MyCustomControl )

' set some custom properties on ctl
panControls.Con trols.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.Con trols.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?

Feb 5 '07 #1
7 2137
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 NumberOfControl s
Dim ctl as MyCustomControl = CType(LoadContr ol(“MyCustomC ontrol.ascx”) ,
MyCustomControl )

' set some custom properties on ctl
panControls.Con trols.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.Con trols.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?
Feb 5 '07 #2
Peter,

I have tried giving the controls a unique ID and then trying to pick up the
control using panControls.Fin dControl("Contr olID") 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
Feb 5 '07 #3
Any controls that are added programmaticall y 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.Fin dControl("Contr olID") 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
Feb 5 '07 #4
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 NumberOfControl s
Dim ctl as MyCustomControl = CType(LoadContr ol(“MyCustomC ontrol.ascx”) ,
MyCustomControl )

' set some custom properties on ctl
panControls.Con trols.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.Con trols.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?
Feb 5 '07 #5
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 programmaticall y 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
Feb 5 '07 #6
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 programmaticall y 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
Feb 5 '07 #7
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
Feb 6 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1012
by: Hendrik W. Hansen | last post by:
I'm working on a WebForm, that through some tabs can load and contain a number of different "plugin" UserControls, all derived from the same "plugin" super class - a bit like the IBuySpy portal. An XML file defines which plugins are available and should be listed in my tab control. When the user clicks on as tab, the page should load the corresponding UserControl into a panel via Page.LoadControl and Controls.Add. This works fine, but...
1
13727
by: Josh | last post by:
Hi Guys, I have been having a big problem with trying to pass parameters into a user control when the user control is dynamically loaded into a placholder. I am developing in c#. I have get and set methods on the user control "editButton.ascx" which work fine. How do i pass parameter into the user controls "c1", "c2" ? Here is a bit of my code that is calling the user control from the
2
1826
by: Ben Fidge | last post by:
Hi I'm writing a web application where the entire site is driven through a single page, and content is dynamically loaded using Web UserControls. This is achieved by using Page.LoadControl() from within the main page. My Login UserControl has a "Login" button and I want that to become the default button when the user presses .
4
3123
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three UserControls into the PlaceHolder's child control collection depending upon which of the three radio buttons is selected. Each of the three UserControls have postback events themselves triggered by button clicks. The problem I'm having is keeping track of...
2
1716
by: Vivek Sharma | last post by:
Hi There, I have a situation where I wish to load the controls dynamically on the basis of user role. Hence, I am using this code. if (UserRole == "IS Administrator") { Control UC1 = LoadControl("../UserControls/ISJob/uctlJobGeneral.ascx");
1
1920
by: dnn | last post by:
How can I access dynamically loaded variables? I am trying to load an external javascript file dynamically and then access its variables. The script is loaded by the onload handler. The code looks like this: var constants = document.createElement("script"); constants.src = REL_PATH + "extensions_demo/js/constants_" + MY_LANGUAGE + ".js"; constants.type ="text/javascript";
5
8601
by: marfi95 | last post by:
I have a form that has a left and right panel. In the left panel is a treeview. The right panel I want to change dynamically based on the type of node selected. What I'm doing is loading the treeview nodes through an XML file. As part of each node in the XML, I'm using an attribute that indicates the name of a sub form to load. As part of all these little child forms, the main control is a panel, which I then assign its parent to the...
7
2154
by: Nathan Sokalski | last post by:
I have a page which I dynamically add several usercontrols (*.ascx files) to using the following code: Public Sub Refresh() For Each section As DataRow In Me.GetSections().Rows CType(Me.FindControl("admin" & CStr(section("buttontext")).Replace(" ", "")), adminsection2).RefreshSection() Next End Sub
2
3123
by: Smithers | last post by:
Using 3.5, I am stuck in attempting to: 1. Dynamically load an assembly 2. Instantiate a class from that assembly (the client code is in a different namespace than the namespace of the dynamically loaded assembly) so far so good (per my code below)... but here is where I'm getting hung up: 3. Call methods of that type (see comments in my code) If the types in the dynamically loaded assembly were in the same namespace
0
9799
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9646
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10795
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10512
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9332
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3083
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.