473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Controls disappear after postback when added to a controlcollectio

I run into a problem where an ASP CheckBoxList control disappears after being
added to a controlcollection. (Have not tested this with other Controls ...)

It takes a WebForm with 3 lines of codes to reproduce the behaviour

Create a WebForm with a CheckBoxList and a Linkbutton. Add a few items to
the list, and add the following code to the LinkButton Click event:

Dim MyctrlCollection As ControlCollection
MyctrlCollection = Me.CreateControlCollection()
MyctrlCollection.Add(FindControl("CheckBoxList1"))

See how the CheckBoxList is gone, after clicking the LinkButton. I have no
idea why it is not rendered anymore ... Please can someone explain?
(Viewstate is Enabled!)

If you add it to e.g. an ArrayList, everything works as expected.
Jul 21 '05 #1
5 3134
pm
Note A Control object can only be assigned to one Control.ControlCollection
at a time. If the Control is already a child of another control it is removed
from that control before it is added to another control.
(From MSDN documentation).

It would be interesting to know the reason why. My guess is has something
to do with reference passing versus pass by value.

"Martin" wrote:
I run into a problem where an ASP CheckBoxList control disappears after being
added to a controlcollection. (Have not tested this with other Controls ...)

It takes a WebForm with 3 lines of codes to reproduce the behaviour

Create a WebForm with a CheckBoxList and a Linkbutton. Add a few items to
the list, and add the following code to the LinkButton Click event:

Dim MyctrlCollection As ControlCollection
MyctrlCollection = Me.CreateControlCollection()
MyctrlCollection.Add(FindControl("CheckBoxList1"))

See how the CheckBoxList is gone, after clicking the LinkButton. I have no
idea why it is not rendered anymore ... Please can someone explain?
(Viewstate is Enabled!)

If you add it to e.g. an ArrayList, everything works as expected.

Jul 21 '05 #2
pm
From MSDN documentation on Controls.ControlCollection.Add

Note A Control object can only be assigned to one
Control.ControlCollection at a time. If the Control is already a child of
another control it is removed from that control before it is added to another
control.

I wouldn't mind knowing the definitive reason why.

"Martin" wrote:
I run into a problem where an ASP CheckBoxList control disappears after being
added to a controlcollection. (Have not tested this with other Controls ...)

It takes a WebForm with 3 lines of codes to reproduce the behaviour

Create a WebForm with a CheckBoxList and a Linkbutton. Add a few items to
the list, and add the following code to the LinkButton Click event:

Dim MyctrlCollection As ControlCollection
MyctrlCollection = Me.CreateControlCollection()
MyctrlCollection.Add(FindControl("CheckBoxList1"))

See how the CheckBoxList is gone, after clicking the LinkButton. I have no
idea why it is not rendered anymore ... Please can someone explain?
(Viewstate is Enabled!)

If you add it to e.g. an ArrayList, everything works as expected.

Jul 21 '05 #3
pm
Note A Control object can only be assigned to one Control.ControlCollection
at a time. If the Control is already a child of another control it is removed
from that control before it is added to another control.

"Martin" wrote:
I run into a problem where an ASP CheckBoxList control disappears after being
added to a controlcollection. (Have not tested this with other Controls ...)

It takes a WebForm with 3 lines of codes to reproduce the behaviour

Create a WebForm with a CheckBoxList and a Linkbutton. Add a few items to
the list, and add the following code to the LinkButton Click event:

Dim MyctrlCollection As ControlCollection
MyctrlCollection = Me.CreateControlCollection()
MyctrlCollection.Add(FindControl("CheckBoxList1"))

See how the CheckBoxList is gone, after clicking the LinkButton. I have no
idea why it is not rendered anymore ... Please can someone explain?
(Viewstate is Enabled!)

If you add it to e.g. an ArrayList, everything works as expected.

Jul 21 '05 #4
> From MSDN documentation on Controls.ControlCollection.Add

I have been looking for such a piece of text ... well, then it IS
documented. Thanks for pointing me to the docs. I haven't found anything
llike that in the .NET help files. All I see in the docs is: Adds the
specified Control object to the collection.

Also, it is just something I did not expect. After all, a control collection
(I thought) is just another collection specially for controls. But as you
already suggest, there's probably more to it ...
Martin

Jul 21 '05 #5
pm
No problem Martin. I suspect the same but haven't used the ControlCollection
much. Thanks for the training :)!

"Martin" wrote:
From MSDN documentation on Controls.ControlCollection.Add


I have been looking for such a piece of text ... well, then it IS
documented. Thanks for pointing me to the docs. I haven't found anything
llike that in the .NET help files. All I see in the docs is: Adds the
specified Control object to the collection.

Also, it is just something I did not expect. After all, a control collection
(I thought) is just another collection specially for controls. But as you
already suggest, there's probably more to it ...
Martin

Jul 21 '05 #6

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

Similar topics

1
by: Mike | last post by:
I create a dynamic control and added it to a panel. I get the selected value of the control via a btn_click() but my problem is everytime the page is postback the selected values go back to the...
2
by: Sam | last post by:
I have a custom control (MyTextBox - taken from Microsoft website) that implements the IPostBackDataHandler interface. It is added to the controls collection of a placeholder control during the...
1
by: Angel | last post by:
I have added controls to the placeholder control. All the controls that were added have EnableViewState = true including the placeholder. One of the controls has a button that performs a postback. My...
1
by: Ryu | last post by:
I am having a problem whereby the controls (along with its values) disappear after I click a button. The controls that I have added is a textbox in a Table. Both of these displayed correctly. Any...
1
by: Adrian Parker | last post by:
At postback time, the controls are all recreated before an event fires. If the event determines that some of the controls need to be replaced with different controls that you can't create as...
5
by: Martin | last post by:
I run into a problem where an ASP CheckBoxList control disappears after being added to a controlcollection. (Have not tested this with other Controls ...) It takes a WebForm with 3 lines of codes...
8
by: mark.norgate | last post by:
I've asked this question before, but still haven't solved it, so am asking again. I am programmatically adding a user control to the page in response to a button click. The user control consists...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
2
by: Raymond Du | last post by:
Hi, I need to find a way to place multiple textboxes and other controls dynamically on a web form, I tried to put a placeholder on a form then create and add controls dynamically into the place...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.