dropdownlists, their items and viewstate

D

Dave Weeden

Hi all,

I have a strange issue with dropdown items that I'm trying to understand.
We use dropdowns in three contexts:

1) directly specified in an aspx file
2) dynamically added to the controls collection of a server side table cell
of an aspx file
3) dynamically added to a custom class derived from WebControl that
implements INamingContainer (not a user control) that is in turn added to
the controls collection of another server side table cell of an aspx file

In each case we populate the option lists in the OnInit method.

In case #1, we noticed duplicate items were getting created on postbacks so
we put in a check so that the items were only populated when IsPostback was
false.

We followed the same methodology for case #2 and #3, figuring that we needed
to avoid creating duplicates.

Turns out the behaviour is quite different in these two latter cases:

Case #2: Does not produce the duplicates on postbacks and in fact will end
up with an empty option list unless it is repopulated each and every time
the page is loaded.

Case #3: Does not produce the duplicates on postbacks but will retain its
option list and therefore it doesn't matter whether the list is populated or
not on subsequent postbacks.

This basically boils down to case #1 needs to populate its list exactly
once, case #2 needs to populate its list every time, and case #3 needs to
populate its list at least once but more than that is ok as well.

The obvious workaround is deal with the behaviour on a case by case basis as
appropriate but I'd really like to understand what is going on behind the
scenes (especially why case #2 and case #3 are different).

Thanks in advance,

Dave
 
T

Teemu Keiski

Hi,

can you provide more information, such as small code examples of each cases?
Are you using ASP.NET 2.0, where AppendDataBoundItems property could explain
the first case?

And with second case, do you do populating before or after adding the
control (/DDL) to Controls collection of TableCell? ViewState is tracked &
stored only after control is added to the Controls collection.

And could third be related to mechanics used in second scenario?
 
D

Dave Weeden

Hi Teemu,

In case #2 I was actually adding the control after I had initialized its
option list, once I changed that around case #2 worked as expected (like
case #1).

As for #3, the way I implemented the custom control added multiple drop
downs prior to adding to their item collections (it's a date picker).

So I guess the view state rules go something like this (when leveraging the
!IsPostBack flag):

1) simple properties can be set before or after adding the object to a
controls collection eg. Text, AutoPostBack etc.
2) non-trivial properties should only be set after adding to the container
controls collection eg. Items collection

Thus the golden rule is "always add your dynamic controls to their container
before trying to set any of their properties".

Thanks for the help!!! It was instrumental in understanding what was going
on.

Cheers,

Dave

PS

Gives me another reason to cheer for Finland at the bronze medal game here
tomorrow!
 

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,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top