how to make requiredfieldvalidator error message display after click submit button

Tony picture Tony · May 18, 2010 · Viewed 60.8k times · Source

now, the error message will display if I move out of current textbox. I don't want to display it until I click submit button.

Answer

Joop picture Joop · May 18, 2010

This isn't possible when ClientScript is enabled for your validators. And the ClientScript is by default enabled for your validators. You need to disable this by settings EnableClientScript to False in your source.

Now in the event handler of your submit button call Page.Validate() and Page.IsValid to see if every validators did pass the test.

Example:

<asp:RequiredFieldValidator ID="rfvFirstName" runat="server" ControlToValidate="txtFirstName" EnableClientScript="false" Display="Dynamic" SetFocusOnError="true" />

Page.Validate();
if (!Page.IsValid)
{
     //show a message or throw an exception
}