Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamically loaded user control 1

Status
Not open for further replies.

crystalized

Programmer
Jul 10, 2000
390
CA
Hi all,

I am dynamically adding a user control to my page. The control needs to be added every time the page loads or it disappears. I can understand this, what I do not understand at all is the problem I am having with it.

The user control contains a couple of web control textboxes. The web control textboxes seem to maintain state over the reloads (the values in them are still in them after a reload of the page). But when I try to access the values of the textboxes within the code behind for the control the values are always blank. I can kind of understand this since the control is technically being reloaded each time - but then why are they keeping the values across reloads?

The real question I have is does anyone know how to get this dynamically loaded control to be able to retrieve the values from it's own text boxes effectively.

Any help would be appreciated. Crystal
crystalized_s@yahoo.com

--------------------------------------------------

Experience is one thing you can't get for nothing.

-Oscar Wilde

 
Man, this problem is a nasty one. The trick is to figure out (and hopefully one day master -- I know I'm not there) the lifecycle of the page. This is particularly bad on dynamically loaded user controls -- and gets even worse when you start nesting them.

short synopsis of the events concerning your problem is:

page_init
load_viewstate
page_load
events

so that if you load the controls in the page_init, instead of the page_load, viewstate is maintained (I know you said that isn't your problem, but it is related) and then you can grab your values from the user controls.

In our projects, we set up properties for the user controls that we can access like any other object, thereby hiding the .selected this and .checked that -- and just:

myControl.checked, which abstracts the aforementioned ugliness into the user control, itself -- making the container's code much more readable.

But I digress. Try loading them up in the page_init event and see if that helps.

:)
paul
penny1.gif
penny1.gif
 
Paul,

Thanks for the response and I will certainly give it a try. I also have properties in the usercontrol - but perhaps I did not make this completely clear. Originally I did not have properties as the user control needs the values but the containing page does not need the values. But even with the properties the user control simply does not recognize that there is a value in the textboxes (unless I use Request.Form which is messy because when the control is dynamically loaded all the textboxes get a prefix tacked onto them)

If you want another bit of issue with this problem it is that I do have a nested user control within the control that I am loading dynamically. This control likewise has textboxes (all hidden) and it does not seem to have any difficulty accessing the values within the textboxes in the control (to return them as properties).

I will let you know if moving the stuff to the page init works though, I sure hope it does it would be a real life saver.
Crystal
crystalized_s@yahoo.com

--------------------------------------------------

Experience is one thing you can't get for nothing.

-Oscar Wilde

 
Well adding it to the init works like a charm, thanks a bunch. Crystal
crystalized_s@yahoo.com

--------------------------------------------------

Experience is one thing you can't get for nothing.

-Oscar Wilde

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top