Today I tried something I thought would be simple: add a validation summary control to my .NET 3.5 screen, inside an update panel. I soon discovered that the validation summary never appeared no matter how much tinkering with standard properties I did on either the validation summary or the range validator I was using.
The solution was adding “onblur=”Page_ClientValidate(‘All’)”" to the control I want to evaluate. In this case, I am validating a text box for a date to be within a certain range, and this text box is the one that needs this code snippet added.
Toward the top of my screen, inside the update panel, is my validation summary. In this case, I wanted a pop-up window instead of the summary, so I set those properties. The only specific thing here to fix my issue is the ValidationGroup of “All”:
<asp:ValidationSummary runat=”server” ID=”valSummary” HeaderText=”Please fix the following errors:” ShowMessageBox=”true” ShowSummary=”false” ValidationGroup=”All”/>
Farther down screen is my textbox. Note the “onblur” property and that the group is set to “All”. Below that control is a calendar extender I happen to be using, and finally, the range validator, which is also set to the ValidationGroup=”All”:
<asp:TextBox runat=”server” ID=”txtAddressVerifiedDate” CausesValidation=”true” onblur=”Page_ClientValidate(‘All’)” ></asp:TextBox>
<cc1:CalendarExtender ID=”CalendarExtender3″ runat=”server” TargetControlID=”txtAddressVerifiedDate”></cc1:CalendarExtender>
<asp:RangeValidator ID=”rvAddressVerified” runat=”server” ControlToValidate=”txtAddressVerifiedDate” ErrorMessage=”Make sure the date entered is not more than 1 year from the current date nor less than 1/1/1993.” MinimumValue=”01-01-2009″ Text=”*” Type=”Date” Visible=”true” ValidationGroup=”All”></asp:RangeValidator>
As a side note, I need my RangeValidator.MaximumValue to be dynamic, so I set that in the Page_Load event like this:
rvAddressVerified.MaximumValue = DateTime.Now.AddYears(1).ToShortDateString();
Also note that the RangeValidator.ErrorMessage is what will appear in the Validation Summary, while the Text property of * will appear next to the actual text box. This not only saves on screen real estate at the textbox, but prevents my carefully laid out HTML from redrawing into an ugly layout due to the large size of the error message.