|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
dropdownlists, their items and viewstateI 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 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? Show quoteHide quote "Dave Weeden" <dave@nospam.com> wrote in message news:uCPUKWWEGHA.1584@TK2MSFTNGP10.phx.gbl... > 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 > 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! Show quoteHide quote "Teemu Keiski" <jot***@aspalliance.com> wrote in message news:u$wiu9WEGHA.984@tk2msftngp13.phx.gbl... > 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? > > -- > Teemu Keiski > ASP.NET MVP, AspInsider > Finland, EU > http://blogs.aspadvice.com/joteke > > > "Dave Weeden" <dave@nospam.com> wrote in message > news:uCPUKWWEGHA.1584@TK2MSFTNGP10.phx.gbl... >> 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 >> > >
How to deselect item on single select listbox?
Where in the viewstate is a GridView's rows? I really need to get this working...... Remove Dynamic User Controls from a collection link on an entire row in a datagrid control exposing collection. Gridview - Accessing a field value in current row HTML editor for Safari browser Customizing a TreeNode's viewstate ImageButton in a datagrid. How do I determine on which row I am? |
|||||||||||||||||||||||