While debugging a postback problem with a DropDownList within a Repeater I discovered that even though everything looked like it was set up correctly the selections from the user on the DropDownList were not being restored from the view state.
My DropDownList was defined as
<asp:DropDownList ID="EmployeeColumnDropDownList" runat="server" Visible="True" />
VS2008 properties inspector indicates that EnableViewState is True Visible is True as can be seen so it should be enabled. Yet stepping through with the debugger and tracing the Page_load, OnItemDataBound and the actual click_event I was interested in showed that the data wasn't being populated from the view state.
The problem I was experiencing was that the selection made by the user wasn't being picked up by my event handling code and it appeared to be reverting to what had been set up at the start of the page render on the initial page_load (which has the obligatory
if (!this.IsPostBack)
wrapper for populating the repeater and dropdown lists.
Inserting a specific enableViewState="True" into my definition as follows solved the problem.
<asp:DropDownList ID="EmployeeColumnDropDownList" runat="server" Visible="True" EnableViewState="True" />
Remember that VS2008 was showing this as True anyway on the properties inspector.
Is this expected behaviour? or have I fluked a solution by forcing a condition through?
I'm asking the question as this seems a little odd and I don't want this code to bite me later by suddenly stopping working.
EDIT to clarify how I am processing the code
I have a few dropdown lists that are generated using a repeater and are all populated from within the OnItemDataBound callback on each loop through the repeater.
I have a button outside the repeater that is hooked up to an onclick event handler. It is this handler which is not reading the correct user selection.
Edit - the viewstate appears to be a red herring
After hours of research and debugging I think the view state is a red herring.
I have a bug in here somewhere that is causing my drop down lists not to retain their selected state on the first postback (caused by a button click), When the form is returned they've lost their selection state (they still have the correct contents).
All subsequent postbacks, using the same button retain the correct selections. All code is the same as far as I can see for all postbacks.
it's as if it isn't merging the request parameters into the view state correctly for the very first postback.
I'm trying to set up a very simple example to dig into this, but it's already lost me nearly a days work so far. I've been looking at merging the data myself but the presence of a repeater in my case is not making things easy.