RequiredFieldValidator doesn't work when submitting form after Posting back by TextChanged event to the Textbox

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 01:07:53

问题


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



来源:https://stackoverflow.com/questions/1182150/requiredfieldvalidator-doesnt-work-when-submitting-form-after-posting-back-by-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!