0

Ok, I'm about to go crazy here, because the reversed definition of insanity is doing the same thing and getting different results.

For the given ASPX:

<asp:DropDownList ID="ddl1" runat="server" DataSource="<%# DataContext.Items1 %>" 
DataValueField="Id" DataTextField="Text" AppendDataBoundItems="false"></asp:DropDownList>
<br/>
<asp:DropDownList ID="ddl2" runat="server" DataSource="<%# DataContext.Items2 %>" 
DataValueField="Key" DataTextField="Value" AppendDataBoundItems="false"></asp:DropDownList>

You would think that both DropDownLists would behave similarly - that is, upon postback, both would go through the same exact lifecycle and work normally. After all, the only difference is that they use different datasources (both IEnumerable<Whatever>) and access different properties for the Text/Value fields.

So I expect that on a postback (via form submit), by the time Page.LoadComplete executes, both the DDLs should have loaded 1) loaded their Items via the DataSource and 2) loaded their state (SelectedIndex) from the postback data.

Unfortunately, that is not the case. What I am seeing is that ddl1 will execute LoadViewState (loads Items) then LoadPostData (sets SelectedIndex), but ddl2 does not execute LoadViewState - only LoadPostData. At which point ddl2 is now incorrect since if it doesn't have Items then it ignores the value from LoadPostData.

So I am flabbergasted. I have cleaned the solution and restarted VS2010 several times, i've added EnableViewState="true" to ddl2, and yet the inconsistent behavior persists.

Can somebody shed some light here as to why the DropDownLists behave differently despite being configured nearly identically?

CLARIFICATION

My page is drastically more complicated than 2 DropDownLists, I only specified 2 in the example for clarity's sake. There's actually 42 DDLs, 7 checkboxes, 5 textboxes, and 2 listboxes

UPDATE

I tried
A) swapping datasources/fields - no diff
B) using the same datasources/fields - no diff
C) Removing ddl1 - ddl2 started working again!
D) Added ddl3 (a copy of ddl1) - neither ddl2 or ddl3 worked

So I think I'm hitting against some sort of ViewState size limitiation here - i'm going to recode some of my DropDownLists such that they don't store the Items in the ViewState, but instead reload it from the DataSource prior to LoadPostData.

PhilChuang
  • 2,556
  • 1
  • 23
  • 29
  • Have you ensured both data sets have the data you expect during the postback? – Brian Dishaw Jun 28 '11 at 18:42
  • @Brian Dishaw both datasets are present - but neither are referenced until Page.PreRender/Databind. The DropDownList is somehow storing its Items in the ViewState which gets loaded prior to Page.Load – PhilChuang Jun 28 '11 at 19:21

3 Answers3

1

The simplest explanation tends to be the most likely to be correct. So if everything is the same except for the data source, then the problem lies in the data source. Alternatively, it works with one so the problem is having two. Have you tried:

  1. loading two drop down lists using the same data source (both the good one and the bad one?)
  2. loading a single drop down list on the page pointing to the bad data source?

Hope this helps.

theoretical
  • 420
  • 2
  • 7
  • I agree: try to isolate one variable at a time and figure out which "moving part" causes the break. – mikemanne Jun 28 '11 at 18:46
  • I did the following: A) swapped datasources and B) made both datasources the same (Items1). In both cases, ddl2 failed to load. Stranger and stranger. – PhilChuang Jun 28 '11 at 19:12
  • ddl2 has different DataValueField and DataTextField values as well. Sorry I didn't elaborate. I assume when you "swapped datasources" that you also swapped DataValueField and DataTextField values? – theoretical Jun 28 '11 at 19:19
  • @theoretical yup, I swapped those values as well – PhilChuang Jun 28 '11 at 19:30
  • does ddl2 work (pointing to either data source) if ddl1 is not on the page? – theoretical Jun 28 '11 at 19:38
  • hmm, removing ddl1 makes ddl2 magically work. Perhaps there is some sort of data size limitation on viewstate? – PhilChuang Jun 28 '11 at 19:50
  • I'm not aware of any explicit limit on ViewState size. I've been on projects with some REALLY large ViewStates, that functioned just fine (they were slow, but worked). There's a firefox addon which will tell you the actual size of your viewstate. – mikemanne Jun 28 '11 at 20:42
0

Similar to @theoretical's answer, another thing to check/try:

Ensure that "Key" and "Value" are defined as properties on DataContext.Items2. Make sure they're not public data fields, but actually are properties; some of the .NET controls (Repeaters, I think) look explicitly and only for Properties when doing reflection.

Maybe temporarily switch ddl2's DataValueField and DataTextField settings to be the same as ddl1, to help narrow down the possible issues.

You've got me curious as to the root cause of this issue - good luck!

mikemanne
  • 3,535
  • 1
  • 22
  • 30
0

I don't know for sure what the problem is, but my guess is that I had 1 too many form fields, which caused ddl2 to not be stored correctly in the ViewState.

So I created a LightDropDownList which subclassed DropDownList and used that for ddl2. The LightDropDownList disables ViewState, and calls DataBind() during OnInit. The way the normal DDL works is that it loads the Items from the ViewState, sets the selected item on LoadPostData, and then the Items get overridden when the Page databinds. So I could eliminate the need to store all the Items data in the ViewState simply by manually calling DataBind. That way when the LoadPostData method gets called, the DDL has its Items loaded and therefore the selected item is correctly set.

PhilChuang
  • 2,556
  • 1
  • 23
  • 29