How do I restore state in a dynamically loaded custom control?

A

Alan Silver

Hello,

I have a products page that takes a product ID in the query string.
Based on the product details (from a database), the page then loads up
one of a number of custom controls, calls a method in the control (that
displays product info) and then loads the control into a placeholder so
it will be displayed.

I want to be able to restore the state of the controls in the custom
control, but can't get it to work. For example, the custom control could
contain a dropdown list for price options for the product (eg, small,
medium and large), and I want to have the user's choice restored after
postback.

Any idea how to do this? I've been searching, but couldn't find an
answer (that I understood or worked!!). TIA
 
S

S. Justin Gengo [MCP]

Alan,

As long as the dropdownlist in the dynamically loaded control has viewstate
set to true then it should retain it's value automatically. All you need to
do is make certain that the control is loaded again and that you don't
re-bind the dropdown (if it's a databound control). If you are databinding
the dropdown just make certain that you only do it on control load:

If Not Page.IsPostBack Then
'---Bind the dropdown
End If

I have some fairly simple examples of using dynamic controls on my website
here:

http://www.aboutfortunate.com?page=codelibrary

Use the search box there to search for "dynamic control". The examples may
help you.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
A

Alan Silver

"S. Justin Gengo said:

Thanks for the reply. I'm still not comletely clear on this, so maybe
you could help me along...
As long as the dropdownlist in the dynamically loaded control has viewstate
set to true then it should retain it's value automatically.

OK, we're OK so far. I haven't set EnableViewState to false for any
controls, so we can safely assume that it's true.
All you need to
do is make certain that the control is loaded again

I presume this means the actual user control itself, not the
dropdownlist? If so, then yes it's loaded again.
and that you don't
re-bind the dropdown (if it's a databound control). If you are databinding
the dropdown just make certain that you only do it on control load:

OK, so this is where my main difficulty arises. Suppose I do it so that
the custom control only binds its child controls on loading and not on
postback, would just loading the custom control from the parent page on
postback be enough? I assumed that the custom control would have to redo
all its work.

I haven't tried this yet as it's a bit complex, given the amount of work
the custom control does. I just wanted to get the concept clear. It
sounds like you are saying that all the info about the custom control is
held in view state, but you still need to load it, without letting it do
anything, to see it n the page.

IS this right?
If Not Page.IsPostBack Then
'---Bind the dropdown
End If

I have some fairly simple examples of using dynamic controls on my website
here:

http://www.aboutfortunate.com?page=codelibrary

Use the search box there to search for "dynamic control". The examples may
help you.

Thanks, I looked there, but the problem of translating your examples
into what I have done confused me. I think if I can clarify exactly how
custom controls are handled on postback, then I may be a lot nearer my
goal.

Thanks again
 
A

Alan Silver

Alan Silver said:
I haven't tried this yet as it's a bit complex, given the amount of
work the custom control does. I just wanted to get the concept clear.
It sounds like you are saying that all the info about the custom
control is held in view state, but you still need to load it, without
letting it do anything, to see it n the page.

Just to clarify, I did try it and it didn't work. I changed it so that
on first load, the custom control sets the text of some Literal
controls, binds a couple of dropdownlists, sets the visibility of
various placeholders, etc. On postback, none of this is done. The result
was that on postback, the control looked as if nothing had been done to
it, ie none of the work that was done during the initial load had been
saved.

I also tried just restricting the actual databinding code for the
dropdown to first load, but that left me without a dropdown.

So obviously I'm missing something here. I would be very grateful if you
could explain further. Thanks
 
J

jhcorey

Well, if this works the same way as dynamically loaded non-custom
controls, then you indeed have to reset everything in the load event
for the page.

That was my task -- I had many controls that might possibly be loaded
dynamically, so I ended up communicating from the control to the page
to save stuff in the page's viewstate (since the control's viewstate
was not going to persist). This included an id of the control that was
loaded, so I could load it again at postback and reset all the data.
At that point events in the control would work.

There's many similar experiences in this newsgroup.

Jim
 
A

Alan Silver

Well, if this works the same way as dynamically loaded non-custom
controls, then you indeed have to reset everything in the load event
for the page.

That was my task -- I had many controls that might possibly be loaded
dynamically, so I ended up communicating from the control to the page
to save stuff in the page's viewstate (since the control's viewstate
was not going to persist). This included an id of the control that was
loaded, so I could load it again at postback and reset all the data.
At that point events in the control would work.

There's many similar experiences in this newsgroup.

The word "bleah" springs to mind!!

I'm sure this is a ripe area for future improvements. This must be a
fairly common scenario, but coding it is a nightmare. I got around the
problem by picking up the values I wanted from Request.Form, where (to
my utter surprise), the control values were stored with their original
names.

I'm now trying to do the same for when the info is in a repeater, and
that's being every bit as stubborn as the first problem!! Ho hum.

Thanks for the reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top