Validation in user controls

I have a page with three user controls. The first control dynamically populates itself with link buttons, depending on the result of a database querry, in this case, ("Save", Cancel").

The second control reads values out of a database table and displays the values as text in an htmltable control.

The third control dynamically populates an html table with text, textboxes and dropdownlists. None of the dropdown lists have a value selected. Under certain conditions it places a RequiredFieldValidator control next to one of the dropdownlists.

When the entire page populates itself and loads in the browser, the validator control text is displayed in red, even before any user input has occured.

When the "Save" button the code checks "Page.IsValid", but it returns true, even if no selection is made.

I want the error message to remain hidden until I click "Save". If no selection is made in the dropdownlist box, I want the error to be visible, and the event handler for the link button to know about it.
[1052 byte] By [bsaucer] at [2007-11-20 8:53:42]
# 1 Re: Validation in user controls
Are you setting the ErrorMessage or Text property with the message you want to display? My experience has been that the Text property is displayed regardless of the validation. It depends on the Visible property. The ErrorMessage property, on the other hand, appears to only display when the validation fails. Another property to look at is the Display property which may contain one of the ValidatorDisplay values. There is also a neat property, SetFocusOnError, which set the focus to the control being validated when the validation fails.

What I do when I have a dropdown list that I want to validate is to have a value with the selectedindex of 0 and selectedvalue of 0 and the text of "Please Select a value". I then use a CompareValidator control to reference this control and I set the Type property to integer, the ValueToCompare property to 0 and the Operator property to NotEqual.

Hope this helps
wmain at 2007-11-9 11:53:24 >
# 2 Re: Validation in user controls
I've partially fixed the problem. I restructured the entire page so that there are no user controls with controls inside them. Everything is now on the main page. The Page_Load event calls routines which dynamically creates all the controls and sets their values (if any). I now have two RequiredFieldValidators each monitoring the ID property of dropdownlists. When I click the "save" LinkButton, the error messages shows up if one of the dropdownlists is not selected. So far so good.

The problem I'm haviong now is that when the "Cancel" button is clicked, the errors are still displayed, and the page won't change. I have the "Save" button set to CausesValidation = true, and all other buttons have CausesValidation = false. I also set the RequiredFieldValidators set to IsValid = true when I create them.

I do notice that when I click the cancel button that Page_Load is called again just before the error messages come on. Does that make any difference?
bsaucer at 2007-11-9 11:54:23 >