Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with custom validator event firing immediately after page load

Status
Not open for further replies.

Michalko

Programmer
Jun 12, 2007
3
US
Gregarious greetings and sincere salutions,

The custom validaor that I coded on one page of this web app works fine. When the page loads, the error messages from the custom validator event about the need to enter data into certain fields do not show. When the Submit button is clicked, the validator's error messages that I coded about the need to enter data into certain fields do show when those fields are empty.

But I did the same for a different page and I'm having a problem. When the page first loads, the code goes from the End Sub line of the page load directly to the beginning of the custom validator's event and therefore the custom error messages about the need to enter data into certain fields show even though the page has loaded for the first time and even though the user has yet to click the Submit button.

Here is the part of the HTML code for this problem. It is the btnAddCriticalIncident and the custom validator valAddCritical.

Various HTML code and then
<tr>
<td>
<asp:panel id="pnlValidation" Runat="server" visible="false">
<table>
<tr>
<td>
<asp:Button ID="btnAddCriticalIncident" runat="server" Text="SUBMIT CRITICAL INCIDENT"
PostBackUrl="~/add_critical_incidents.aspx" ValidationGroup="valAddCriticalIncident" />
</td>
</tr>
<tr>
<td>
<asp:CustomValidator ID="valAddCritical" runat="server"
ErrorMessage="CustomValidator"
ValidationGroup="valAddCriticalIncident" ForeColor="gray"></asp:CustomValidator>
</td>
</tr>
</table>
</asp:panel>
</td>
et cetera

Inside of the page load, there is code in the Else part of an If to open the above panel.

Then I of course have code in the custom validator to force the user to enter data into certain fields, ensure that future dates are not used, et cetera (Protected Sub valAddCritical_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles valAddCritical.ServerValidate). The Submit button has CausesValidation set to True, so when Submit is clicked the custom validation should occur and indeed does occur when the Submit button is clicked.

I have debugged line by line and I don't know why the program goes from the End Sub line of the VERY FIRST Page Load directly to the beginning of the Protected Sub valAddCritical_ServerValidate.

This is in VB.NET 2005 (Page Language = VB).

Any ideas would be greatly appreciated.
 
Without looking at the code, I notice that the CustomValidator is missing the ControlToValidate and OnServerValidate attributes. Is VB hooking the latter up automatically?

Also, it sounds like what you really want is a set of RequiredFieldValidators with a common ValidationGroup. Why are using a CustomValidator?

MCP, MCTS - .NET Framework 2.0 Web Applications
 
BoulderBum, thanks very much for your reply.

I wanted to use my own code in the custom validator to force the user to enter data into certain fields along with my code that ensures that certain date fields don't have future dates, that data entered for dates and times are in valid formats, et cetera. I guess that I could use the RequiredFieldValidators for the part of the validation that forces data to be entered into certain fields.

The ControlToValidate is missing because my Submit button has the same ValidationGroup as the CustomValidator and therefore when the Submit button is clicked the CustomValidator routine runs automatically. When I click the Submit button, the code within the CustomValidator does run.

I just don't know why after the very first page load, the program goes from the very end of the page load event to the beginning of the CustomValidator code and runs the validation. The validation should not occur until the Submit button is clicked (and this button has the same validation group as the custom validator and therefore when the button is clicked the program does go successfully to the validation code).

Thanks very much for replying.
 
No problem.

If you're simply checking for a future date in addition to "requiredness", you'll probably want to use a CompareValidator along with a RequiredFieldValidator instead of your CustomValidator.

If you only want the validation to occur on the server side, you can turn EnableClientScript off.

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top