dynamically adding databound controls

T

trullock

Hi,

I have a usercontrol which contains two dropdown lists and a textbox.
One of the dropdowns is databound, the other is a static list.

i add this control to my page via a button click.

Inorder to make the control remain on the page after subsequent
postbacks im adding the control again in the page load (is this
right?)

Because i have to readd the controls, i have to re-databind the
dropdown in the control. This causes it to forget which value it was
set to. The other dropdown and the textbox remember their selected
value and text properties respectively.

How can i make the first dropdown remember it's setting?

If i save the selected index into the controls viewstate when it is
changed, its not remembered when the controls re-added. Im guessing
thats because its a new instance of the control, but the other
dropdrop and textbox remember their values :s

I know i could use the session but thats messy.

Thanks for any advice
 
L

Lloyd Sheen

Hi,

I have a usercontrol which contains two dropdown lists and a textbox.
One of the dropdowns is databound, the other is a static list.

i add this control to my page via a button click.

Inorder to make the control remain on the page after subsequent
postbacks im adding the control again in the page load (is this
right?)

Because i have to readd the controls, i have to re-databind the
dropdown in the control. This causes it to forget which value it was
set to. The other dropdown and the textbox remember their selected
value and text properties respectively.

How can i make the first dropdown remember it's setting?

If i save the selected index into the controls viewstate when it is
changed, its not remembered when the controls re-added. Im guessing
thats because its a new instance of the control, but the other
dropdrop and textbox remember their values :s

I know i could use the session but thats messy.

Thanks for any advice

You have to load your control in the page Init event so that the viewstate
for the control can be setup on postbacks. This is a general thing that
must be done for all dynamically loaded controls.

What you are doing is writing over the info stored in the viewstate and used
for the dropdown events. If you rebind you will loose the selected index as
you are seeing.

Hope this helps
Lloyd Sheen
 
B

bruce barker

the advise is right, but the reason is not correct. the selected index is
not in viewstate (the last one is if viewstate enabled, which normally it
shouldn't be). viewstate is encrypted and can not be modified by the browser.

between oninit and onload is an event called LoadPostbackData (which
contains the new vlaue of the select). on postback the browser sends the
value of the underlying select. so if you databind in oninit, then value will
exists in the select list, so that when the postback data is applied, the
selectedindex is set.

-- bruce (sqlwork.com)
 
T

trullock

the advise is right, but the reason is not correct. the selected index is
not in viewstate (the last one is if viewstate enabled, which normally it
shouldn't be). viewstate is encrypted and can not be modified by the browser.

between oninit and onload is an event called LoadPostbackData (which
contains the new vlaue of the select). on postback the browser sends the
value of the underlying select. so if you databind in oninit, then value will
exists in the select list, so that when the postback data is applied, the
selectedindex is set.

-- bruce (sqlwork.com)

Hi,

thanks for all the info :)

Turns out I was being an idiot. Although it seemed to be forgetting
the selected value, it wasnt. I accidentally had duplicate values in
the dropdown, and coincidentally the first item in the list had the
same value as several id been selecting! doh!

And I thought i needed to load the controls again oninit, but onload
seems to work!

Andrew
 

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