Validation on page load treats SharePoint field controls as empty
-
16-10-2019 - |
Pergunta
On a particular custom list item form I am making for my WSS 3.0 web app, I am required to validate the page when it is loaded up, so that any mistakes the user makes from a previous save are noted immediately. I currently do it by using Page.Validate()
in the OnLoad
method.
With all of the TextBox
and RadioButtonList
controls I populate (also on page load), I can simply populate them before I trigger a bunch of InputFormCustomValidator
controls on the page. For those controls, everything is clear and the correctly filled out fields are not marked as invalid.
But for the UserField
and DateTimeField
controls, it will treat them as empty for the page load validation whether or not they have data in them. This occurs both when using the actual UserField
and DateTimeField
controls that automatically fetch data based on the item ID querystring, and when using the underlying PeopleEditor
and DateTimeControl
controls which I prepopulate on page load like the text boxes.
Once there's been a post-back that calls for validation, the validation will properly recognize the contents of those fields.
Is there something I can do to get around this, so that correctly filled User and DateTime fields are not marked as invalid on page load? Should I run the validation in a different method than OnLoad
? I'd personally like to keep the fields as UserField
and DateTimeField
rather than using the underlying controls, but that restricts me to not being able to define when they get populated. If the solution is to move the control pre-population to an earlier stage in time, I can settle for that, but it remains preferable not to have to do so if it can be fixed in another way.
Solução
Have you tried calling EnsureChildControls() prior to the .Validate()? Also, if you are creating/loading controls dynamically, that is usually done in the CreateChildControls() event rather than the OnLoad() as most custom controls need to participate in the events prior to Load().
Outras dicas
Perhaps try moving Page.Validate() call to the OnPreRender event?