469,282 Members | 1,977 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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

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

--
Alan Silver
(anything added below this line is nothing to do with me)
Feb 28 '06 #1
5 2169
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
"Alan Silver" <al*********@nospam.thanx.invalid> wrote in message
news:sN**************@nospamthankyou.spam...
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

--
Alan Silver
(anything added below this line is nothing to do with me)

Feb 28 '06 #2
In article <#k**************@TK2MSFTNGP10.phx.gbl>, "S. Justin Gengo
[MCP]" <justin@[no_spam_please].invalid> writes
Alan,
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

--
Alan Silver
(anything added below this line is nothing to do with me)
Feb 28 '06 #3
In article <TR**************@nospamthankyou.spam>, Alan Silver
<al*********@nospam.thanx.invalid> writes
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

--
Alan Silver
(anything added below this line is nothing to do with me)
Feb 28 '06 #4
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

Feb 28 '06 #5
In article <11**********************@u72g2000cwu.googlegroups .com>,
jh*****@yahoo.com writes
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.

--
Alan Silver
(anything added below this line is nothing to do with me)
Feb 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by cefrancke | last post: by
2 posts views Thread by DesignerX | last post: by
1 post views Thread by Michael | last post: by
reply views Thread by Earl Teigrob | last post: by
7 posts views Thread by =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.