RequiredFieldValidator have to click twice

左心房为你撑大大i 提交于 2019-11-29 05:13:47

You could add EnableClientScript=false to the validator.

That prevents the client-side validation, so you will always get a postback (which may not exactly be what you want, though). The validation will still be done, just server-side.

Be sure to wrap the button-click logic in a if (Page.IsValid) { ... }, to check the status of the validators.

Apologies for not posting code previously I assumed this to be a standard problem with the RequiredFieldValidator, but have since realised my particular problem was coming from a CompareValidator, used to ensure entered passwords matched.

The CompareValidator was causing the issue that I described, causing me to have to click away from the field to blur and validate, before being able to click on the post button.

I'm not sure why but changing the Display of the CompareValidator from Dynamic to Static has cleared the problem up.

If the validator is Display="Dynamic", and the appearance of the error message causes the submit button to move, then the MouseUp event is not received by the submit button. In this case, the validation fires and clears the error message, but the submit button does not fire. To solve the problem, either set the the validators to be Display="Static", or rearrange the form so that the submit button does not move when error messages appear.

Here's a way to reserve about one, vertical line of space for a dynamic validation message:

<div style="height:1.5em;overflow:visible;"> <asp:RequiredFieldValidator ID="R1" runat="server" ErrorMessage="Name is required" ControlToValidate="TextBoxName" Display="Dynamic"></asp:RequiredFieldValidator> </div>

I did not find it necessary to set EnableClientScript="false", although that did help for a CustomValidator that had no client-side validation function implemented.

Posting your code is always a good idea, That way we could run your code in a test environment and modify it to ensure it works before posting our answer.

I would suggest adding

causesValidation="true" 

to your button to see if that works.

I have a better idea.

Add Text="" to textbox Control.
Add InitialValue="" to Validator Control.
What it will do, when it will be posting, it will find the value of the text box is still the initail value and it will throw an error and the form will not be posted.

Try this:

<asp:RequiredFieldValidator ID="reqFieldCloseComment" ControlToValidate="tbCloseComment" ValidationGroup="ChangeStatus" ErrorMessage="Please enter a reason" Display="Dynamic" runat="server" InitialValue=""></asp:RequiredFieldValidator>
            <asp:TextBox ID="tbCloseComment" runat="server" CausesValidation="true" TextMode="MultiLine" Height="107px" Width="400px" Text=""></asp:TextBox>

        <asp:Button ID="btnCloseRequestFinal" Text="Finish" CssClass="CloseReqButton" runat="server" ValidationGroup="ChangeStatus" />

Here is code that is working fine for me and helping to get rid of double click.

  <asp:TextBox ID="TextBox1" runat="server" autocomplete="off" 
        Enabled="true" MaxLength="20" onfocus="SetActiveControl(this);" Text=""
        CausesValidation="true"  />

<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
    runat="server" ControlToValidate="TextBox1" Display="Static" ErrorMessage="Ha!" SetFocusOnError="True" EnableClientScript="true" ForeColor="" InitialValue="" />
$(function() {
$("input.btn").on("click",function(){
if(Page_BlockSubmit == true) {Page_BlockSubmit = false};
})
});

Page_BlockSubmit is a JS variable defined by the js generated from code behind when you define the validator . I haven't went deeper to know why MS need this variable, but the scenario is:

the first click will make Page_BlockSubmit become false. the second click will check the value of Page_BlockSubmit and return true.

if you implemented the code I posted, every time you click the button, the variable will be set as false which will trigger the submit at every click.

And, you can use google chrome to trace the value of Page_BlockSubmit.

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