A bit of perspective, a bit of a quandry, a bit of a question today.
SharePoint web parts work fine with validation controls. If you just want to display a validation like any other you would do it (using a ValidationSummary control or the validation control itself) things work fine. Just add them to the Controls property of the Web Part and render them. No worries.
Recently I had a fairly large form that contains half a dozen fields that need to be validated. Luckily they're all simple validations (required fields only). When you have a SharePoint form (using the built-in ListForm Web Part) it displays all the controls and a red "*" where required fields are. If you try to save the form without filling out any of the fields two things happen. First you get a client side message saying something like "You must specify a non-blank value for <fieldname>" (where field name is the Display Name of the field that's required). Second, it scrolls to where the control is (and if I remember correctly sets the focus to the control for entry). So here I am trying to mimic this behavior and running into some head-scratching activities.
The way I currently started my cloning process was:
- Create a RequiredValidator control and attach it to my control I want to validate (a TextBox) in my Web Part
- Create a ValidationSummary control and set the ShowMessageBox property to true and ShowSummary property to false. This will popup a client side message box that displays whatever the error message for the control is.
- Set the ValidationSummary.CssClass property to "hidden" to prevent it from being seen in the Web Part
- Render the ValidationSummary control in the RenderWebPart method
Anyone got some perspectives on doing validations in Web Parts. While it may look like madness, trying to mimic what SharePoint does, there is method to it. I would prefer to have a consistent view of error messages throughout SharePoint, both stock Web Parts and custom ones, so the users don't get all peeved that our Web Parts show it one way but when working in a list or doclib they get treated differently. Or maybe I'm just kidding myself here and it's not worth the effort.