RequiredFieldValidator doesn't work when submi

2019-07-16 13:12发布

问题:

In my .aspx form I have a text box with Autopostback=True. I also set a RequiredFieldValidator next to it. But when Posting back by TextChanged event of my text box, my RequiredFieldValidator appears but still I can submit my form with nothing in the text box!

What is wrong, and how can I fix this?

回答1:

You have to set the textbox's CausesValidation property to true (it's set to false by default for textboxes).



回答2:

Give both the text box and the submit button the same validation group.



回答3:

try this code

 <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  <asp:RequiredFieldValidator ErrorMessage="*" ForeColor="Red" ControlToValidate="TextBox2" Display="Dynamic" runat="server" />
    <asp:Button ID="btn" Text="button" CausesValidation="true" runat="server"/>

and make sure that ControlToValidate="" has same name as the <asp:texbox/>has

the best way to do is just right below TextBox field type <asp:Required and when the options come just pres TAB Button twice