Monday, April 8, 2013

SharePointDVDropDown Field not saving correct value and EnableViewState=”false”


The buildup: 
I had a SharePoint list form with more than 20 fields. For a form field of type look-up, I had removed the SharePoint out-of-box field and put a  SharePointDVDropdown. To this I attached a data-source from another list (this is great trick to filter the default drop-down which appears for look-up columns).
I had few date fields as well. 

The strange behavior: 
I select a value from the SharePointDVDropdown say option1. Give an invalid entry in date field. Submit the form.SharePoint validates the date field and returns error as Invalid format. Now I change the value in SharePointDVDropdown to say option2. Give a valid date and submit.

I go to the list and check the item created, the value saved in field corresponding to the SharePointDVDropdown is option1 !!!!! instead of option2. 

The culprit:    
After lot of painstaking hours and hair pulling the culprit was found. There is a property called  EnableViewState in SharePointDVDropDown. If set to "true" this little thing saves the value of the field between server requests. Now since the calendar field gets validated at server side, the value option1 remains saved even if the value is changed from the drop down.

The solution:
While using SharePointDVDropDown field remember to put EnableViewState=”false”. This is because , if there is any server side validation then the value entered before the validation is done, persists in the dropdown and even if the value is changed , it still takes the previous value as input.