For those that don’t want to read the details, if you are trying to let .Net manage your viewstate for dynamic input values, it helps to provide an ID so that the control does not have any problems binding the values to the input control.
I had an interesting issue today. I have a custom server control that consisted of a couple jquery widgets and a hidden field. The hidden field client ID was passed to the jquery widget with the intent of getting the value that was set when the page was posted back. When the page it was on clicked the save button on the page (doing a standard postback), everything worked as expected. I was able to extract the value and not lose input items when the form couldn’t be saved because of some error.
Here is where it gets interesting. The page was documenting information on a patient. If the form was filled out without picking a patient, you should be able to pick a patient without losing the information you typed in. In this case, a modal dialog appears, getting its patient list from a web service. You then click a link that triggered the onchange event (which posted the information back to the server and went through normal lifecycle). This time, however, the viewstate was not maintained and the value in the hidden field could not be maintained. To make things stranger, the value was in the Request.Form collection. It was behaving as if the control was recreated after the load step. I was newing up the control in the field section. Should be instantiated before even OnInit is called. Yet the ClientID in the Request collection was different than the one that was missing it’s value.
As a last ditch effort before I hacked away and manually bound the Request value to the hidden value, I added an ID property. I was thinking it would be easier to see what was going on. I also thought that with the same ID, I would either get an error that the control was already created (which might be a clue) or it would be able to bind to the Request object because the ID shouldn’t change.
Well, victory was mine. The control magically bound to its value as expected.