473,394 Members | 1,761 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,394 software developers and data experts.

Problem access usercontrol added to placeholder

Hi

I have this that adds some usercontrol (UCTodays.ascx) to a placeholder

foreach(A a in B){

UCTodays ucline = (UCTodays )LoadControl("UCTodays.ascx");
ucline.Initializecontrol(line,alternate);
Placeholder1.Controls.Add(ucline);
}

On the mainpage i have a save button when pressed much call the add
usercontrol“s SaveValues() method which of course is public.

If I had manually added the controll then this would be easy. But how do i
get access the controls added? I dont now their id. I have tried
Page.findcontrol("_ctl0") etc. but I dont get the usercontrol.

I have also tried to retain a collection of references to the usercontrols
added but when I call these references i get a usercontrol which contains
the oldvalues before postback.

Can“t i do this?

Anders, Denmark
Nov 19 '05 #1
9 2430
Anders,

You are most likely accessing the control too early. I would suggest
tracing out when your controls are loaded and when you are attempting to
access the data within. You may need to create some sort of event to notify
the observer that the data has been loaded.

Thanks,
Ian Suttle
http://www.IanSuttle.com
http://www.NewsFuel.com - Aggregated Technology News
"Anders K. Jacobsen [DK]" wrote:
Hi

I have this that adds some usercontrol (UCTodays.ascx) to a placeholder

foreach(A a in B){

UCTodays ucline = (UCTodays )LoadControl("UCTodays.ascx");
ucline.Initializecontrol(line,alternate);
Placeholder1.Controls.Add(ucline);
}

On the mainpage i have a save button when pressed much call the add
usercontrolĀ“s SaveValues() method which of course is public.

If I had manually added the controll then this would be easy. But how do i
get access the controls added? I dont now their id. I have tried
Page.findcontrol("_ctl0") etc. but I dont get the usercontrol.

I have also tried to retain a collection of references to the usercontrols
added but when I call these references i get a usercontrol which contains
the oldvalues before postback.

CanĀ“t i do this?

Anders, Denmark

Nov 19 '05 #2
> You are most likely accessing the control too early. I would suggest
tracing out when your controls are loaded and when you are attempting to
access the data within. You may need to create some sort of event to
notify
the observer that the data has been loaded.


Im not quite sure i follow. To recap.

I have my main page. To this i add usercontrols which contains textboxes
with initial values. Now user changes these values in the usercontrols which
was added to the mainpage through a placeholder.

Now the user push a save button on the main page.

1. How do a GET the reference to these usercontrols?

2. Reagarding that i access the usercontrol before load. The usercontrols
are loaded correctly, and I guess the new values in the usercontrols
textboxes should be available in alle events on the mainpage...right?

Anders

Nov 19 '05 #3
Anders,

Sorry for not being more specific the first time.

You may be attempting to access the user control data before the viewstate
information has been updated to the controls. If this were the case, you
would get the default values of the user control instead of the updated input.

To ensure that your timing is correct, add an event handler to the main page
that subscribes to the user control's save button (you may have to declare
the button in the control as public). The code would resemble the following:

this.userControlName.btnSave.Click += new
System.EventHandler(this.handleSaveClick);

With this example, the parent page has a function called handleSaveClick
that will get called when the user control's save button is clicked. You can
then access the information in the user control at the proper time.

If this is still a bit confusing, don't feel bad. Let me know if you
require further explanation.

Thanks,
Ian Suttle
http://www.IanSuttle.com
http://www.NewsFuel.com - Aggregated Technology News

"Anders K. Jacobsen [DK]" wrote:
You are most likely accessing the control too early. I would suggest
tracing out when your controls are loaded and when you are attempting to
access the data within. You may need to create some sort of event to
notify
the observer that the data has been loaded.


Im not quite sure i follow. To recap.

I have my main page. To this i add usercontrols which contains textboxes
with initial values. Now user changes these values in the usercontrols which
was added to the mainpage through a placeholder.

Now the user push a save button on the main page.

1. How do a GET the reference to these usercontrols?

2. Reagarding that i access the usercontrol before load. The usercontrols
are loaded correctly, and I guess the new values in the usercontrols
textboxes should be available in alle events on the mainpage...right?

Anders

Nov 19 '05 #4
> Sorry for not being more specific the first time.

No problem!
You may be attempting to access the user control data before the viewstate
information has been updated to the controls. If this were the case, you
would get the default values of the user control instead of the updated
input.
Ok
To ensure that your timing is correct, add an event handler to the main
page
that subscribes to the user control's save button (you may have to declare
the button in the control as public). The code would resemble the
following:
But. Mu user control dont have a save button. The problem is that they are
self contained. Each usercontrol fetch its own data from a datastore into
its textboxes. Now. if the user presses the one and only save button on the
mainform (where the usercontrols are added to) i need to comunicate to the
usercontrol ( by its public SaveData() method ). My problem is. If I held
the references to the usercontrol in an arrayList of usercontrols references
(In a session var )and called these on OnSaveClick, so when debugging its
textbox value was not updated. Maybe because of the viewstate problem you
refered to.

But again. the save button is the main page which has the placeholder which
has the usercontrols.

Thanks for your time until now!

Anders, Denmark

this.userControlName.btnSave.Click += new
System.EventHandler(this.handleSaveClick);

With this example, the parent page has a function called handleSaveClick
that will get called when the user control's save button is clicked. You
can
then access the information in the user control at the proper time.

If this is still a bit confusing, don't feel bad. Let me know if you
require further explanation.

Thanks,
Ian Suttle
http://www.IanSuttle.com
http://www.NewsFuel.com - Aggregated Technology News

"Anders K. Jacobsen [DK]" wrote:
> You are most likely accessing the control too early. I would suggest
> tracing out when your controls are loaded and when you are attempting
> to
> access the data within. You may need to create some sort of event to
> notify
> the observer that the data has been loaded.


Im not quite sure i follow. To recap.

I have my main page. To this i add usercontrols which contains textboxes
with initial values. Now user changes these values in the usercontrols
which
was added to the mainpage through a placeholder.

Now the user push a save button on the main page.

1. How do a GET the reference to these usercontrols?

2. Reagarding that i access the usercontrol before load. The usercontrols
are loaded correctly, and I guess the new values in the usercontrols
textboxes should be available in alle events on the mainpage...right?

Anders

Nov 19 '05 #5
Ah, I see your issue now. You are definately dealing with a timing issue.
My suggestion would be to try creating an event handler for the Load event of
the user control from the parent page. You know that the view state
information has been updated at that point. Check out this list of events in
order of execution (taken from someone else):

1. Instantiate
2. Initialize
3. TrackViewState
4. LoadViewState (postback)
5. Load postback data (postback, IPostBackDatahandler.LoadPostdata)
6. Load
7. Load postback data for dynamical controls added on Page_Load
8. Raise Changed Events (postback, IPostBackDatahandler.RaisePostDataChanged)
9. Raise postback event (postback, IPostBackEventHandler.RaisePostBackEvent)
10.PreRender
11. SaveViewState
12. Render
13. Unload
14. Dispose

If you create an event handler in the main page for the Load event, you
could then call the SaveData function at that time and you should have the
updated information. An example of this event declaration would look like:

myControl.Load += new System.EventHandler(this.Control_Load);
As a separate suggestion, when loading the user control dynamically, perhaps
set some sort of flag (property, variable, etc) in the user control. Then in
the Load event of the user control, check for the flag and process
accordingly.
Let me know if this doesn't cover it for you.

Ian Suttle
http://www.NewsFuel.com

"Anders K. Jacobsen [DK]" wrote:
Sorry for not being more specific the first time.


No problem!
You may be attempting to access the user control data before the viewstate
information has been updated to the controls. If this were the case, you
would get the default values of the user control instead of the updated
input.


Ok
To ensure that your timing is correct, add an event handler to the main
page
that subscribes to the user control's save button (you may have to declare
the button in the control as public). The code would resemble the
following:


But. Mu user control dont have a save button. The problem is that they are
self contained. Each usercontrol fetch its own data from a datastore into
its textboxes. Now. if the user presses the one and only save button on the
mainform (where the usercontrols are added to) i need to comunicate to the
usercontrol ( by its public SaveData() method ). My problem is. If I held
the references to the usercontrol in an arrayList of usercontrols references
(In a session var )and called these on OnSaveClick, so when debugging its
textbox value was not updated. Maybe because of the viewstate problem you
refered to.

But again. the save button is the main page which has the placeholder which
has the usercontrols.

Thanks for your time until now!

Anders, Denmark

this.userControlName.btnSave.Click += new
System.EventHandler(this.handleSaveClick);

With this example, the parent page has a function called handleSaveClick
that will get called when the user control's save button is clicked. You
can
then access the information in the user control at the proper time.

If this is still a bit confusing, don't feel bad. Let me know if you
require further explanation.

Thanks,
Ian Suttle
http://www.IanSuttle.com
http://www.NewsFuel.com - Aggregated Technology News

"Anders K. Jacobsen [DK]" wrote:
> You are most likely accessing the control too early. I would suggest
> tracing out when your controls are loaded and when you are attempting
> to
> access the data within. You may need to create some sort of event to
> notify
> the observer that the data has been loaded.

Im not quite sure i follow. To recap.

I have my main page. To this i add usercontrols which contains textboxes
with initial values. Now user changes these values in the usercontrols
which
was added to the mainpage through a placeholder.

Now the user push a save button on the main page.

1. How do a GET the reference to these usercontrols?

2. Reagarding that i access the usercontrol before load. The usercontrols
are loaded correctly, and I guess the new values in the usercontrols
textboxes should be available in alle events on the mainpage...right?

Anders


Nov 19 '05 #6
> As a separate suggestion, when loading the user control dynamically,
perhaps
set some sort of flag (property, variable, etc) in the user control. Then
in
the Load event of the user control, check for the flag and process
accordingly.


Oh my. You did it! Simple but effective! It now works like a charm.

I really appriciate your help! Other newsgroups could learn from this groups
profesionalism.

Thanks again Ian, I was almost giving up my appoach

Anders Jacobsen
Nov 19 '05 #7
Great! Good luck with the rest of the project.

Ian Suttle
http://www.NewsFuel.com
http://www.IanSuttle.com

"Anders K. Jacobsen [DK]" wrote:
As a separate suggestion, when loading the user control dynamically,
perhaps
set some sort of flag (property, variable, etc) in the user control. Then
in
the Load event of the user control, check for the flag and process
accordingly.


Oh my. You did it! Simple but effective! It now works like a charm.

I really appriciate your help! Other newsgroups could learn from this groups
profesionalism.

Thanks again Ian, I was almost giving up my appoach

Anders Jacobsen

Nov 19 '05 #8
If you have sample code for how you got this working it would be
greatly appriciated. Thanks!

Anders K. Jacobsen [DK] wrote:
As a separate suggestion, when loading the user control dynamically, perhaps
set some sort of flag (property, variable, etc) in the user control. Then in
the Load event of the user control, check for the flag and process
accordingly.
Oh my. You did it! Simple but effective! It now works like a charm.

I really appriciate your help! Other newsgroups could learn from this

groups profesionalism.

Thanks again Ian, I was almost giving up my appoach

Anders Jacobsen


Nov 19 '05 #9
> If you have sample code for how you got this working it would be
greatly appriciated. Thanks!


I later on acounted problems after been working with the solutions. It“s
seems its quite hard to get the timings right when you need to speak TO the
dynamic added usercontrol. I never got it REALLY workrng. Always a new issue
when sloved another so a went a way from the model and implemented it in a
Repeater.

The logic for each of my lines was quite complex so I wanted to represent
each row as a usercontrol.

But sorry. Havent any code which can do it right.

Anders
Nov 19 '05 #10

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

Similar topics

2
by: Lars Pedersen | last post by:
My app is adding a usercontrol at runtime. How is it possible to remove this usercontrol? Have tried Page.Controls.Remove(UserControl);, but that wont do the trick - any suggestions? -Lars
2
by: bill yeager | last post by:
When trying to run my web project, I get the following error: Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the...
7
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically...
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: Kris van der Mast | last post by:
Hi, been a while since I posted a question myself instead of trying to help others out. I'm refactoring an existing web app that uses dynamic loading of user controls and a lot of...
6
by: Valeri Hristov via .NET 247 | last post by:
I have a problem with a ASP.NET web site. I?m using Windows2000Server SP4, .NET Framework 1.1. The problem is that sometimes(relatively seldom) when browsing the Administration Console(see below) a...
2
by: Tim_Mac | last post by:
hi, i have an aspx page which dynamically loads a user control and adds it to a placeholder. the control is recreated and added to the placeholder for postbacks as well. the user control...
0
by: anandv81 | last post by:
Hi, I encountered a strange problem while working on an application, the problem goes like this. I am generating a few textboxes at runtime at the server side and added to a placeholder, a...
2
by: boomcreation | last post by:
Hi, When I load a userControl, I check if I have a variable session. If my variableSession1 = "A" and do nothing. but if my variableSession1 = "B", I want clear the html in my control and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.