By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,662 Members | 929 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,662 IT Pros & Developers. It's quick & easy.

Problem access usercontrol added to placeholder

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
> 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 discussion thread is closed

Replies have been disabled for this discussion.