简体   繁体   中英

Required Field Validator always shows the error message

I have this code:

<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rvFirstName" runat="server" 
 ErrorMessage="Enter First Name" 
ForeColor="Red" ControlToValidate="txtFirstName" 
SetFocusOnError="True"></asp:RequiredFieldValidator>

The problem is that the error message shows every time the page loads not only after the submit button has been clicked. I want it to show it only if the user tries click the "Next"(submit) button (how it should work).

If this is relevant: The code above is placed in a UserControl which is included in another UserControl(the "Next" button is here) which is then included in a View of a MultiView.

Any ideas

use causeValidation to false on other buttons.

and display to dynamic.

Display="Dynamic" on require filed validator

<asp:RequiredFieldValidator ID="rvFirstName" runat="server" Display="Dynamic"
     ErrorMessage="Enter First Name" ForeColor="Red" 
     ControlToValidate="txtFirstName" SetFocusOnError="True">
</asp:RequiredFieldValidator>

最好的方法是在Button和RequiredFieldValidator上设置ValidationGroup。

please take a look on references below, is it you have something like Page.Isvalid in your page?

References : Required Field Validator, displaying on initial page load

<asp:ValidationSummary ID="ValidationSummary1" runat="server" 
        ShowMessageBox="True" DisplayMode="BulletList" 
        HeaderText="Validation issues" ShowSummary="False" ValidationGroup="Validation"/>


<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>

   <asp:RequiredFieldValidator ID="rvFirstName" runat="server" ErrorMessage="Enter First Name" ForeColor="Red" ControlToValidate="txtFirstName" SetFocusOnError="True" ValidationGroup="Validation"></asp:RequiredFieldValidator>

Add the above code and modify your Required Field Validator

为RequiredFieldValidator和Next按钮设置'ValidationGroup'

删除SetFocusOnError =“True”并在requiredfieldvalidation中添加display =“Dynamic”而不是SetFocusOnError =“True” ,并在页面中设置casevalidtion =“false”

  1. 01.If you want to display message in page load. protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Page.Validate(); } }

    1. Display="Dynamic"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM