Required field validator not working when OnClientClick is added in the button
Asked Answered
S

1

13

hi i have a RequiredFieldValidator Like this

<asp:TextBox ID="txtEmployeeID" runat="server" MaxLength="255" CssClass="txt" 
    OnTextChanged="txtEmployeeID_TextChanged" AutoPostBack="True" 
    ValidationGroup="Save" ></asp:TextBox>     
<asp:RequiredFieldValidator ID="rfvEmployeeID" runat="server" 
    ErrorMessage="Employee ID is required information."
    ControlToValidate="txtEmployeeID" Display="None" ValidationGroup="Save"
    SetFocusOnError="True"></asp:RequiredFieldValidator>

and a button like this

 <asp:Button ID="btnBlockUser" runat="server" Text="Block User" 
    CssClass="submitBtn " OnClick="btnBlockUser_Click" 
    OnClientClick="javascript:return confirm('Are you sure want to Block this user ?')"
    ValidationGroup="Save" />

Now the problem is that if i remove the OnClientClick in the button the RequriedFieldValidator works fine if i put it back there page posts back without showing any error message can some one explain why this is happenening??

Somerville answered 14/6, 2013 at 9:40 Comment(2)
i found out that i could add this Page_ClientValidate() to the OnClientClick event but is it possible to avoid calling the Confirm() method if the Validation fails??Somerville
I did this and it works fine but.. the error message is shown twice OnClientClick="javascript:if(Page_ClientValidate())return confirm('Are you sure want to Block this user ?');"Somerville
F
32

try to use this code it will help you

    <asp:RequiredFieldValidator ID="rfvEmployeeID" runat="server" ErrorMessage="Employee ID is required information."
        ControlToValidate="txtEmployeeID"  ValidationGroup="Save" SetFocusOnError="True"></asp:RequiredFieldValidator>
        <br />
    <asp:Button ID="btnBlockUser" runat="server" Text="Block User" CssClass="submitBtn"  CausesValidation="true"  OnClientClick="return validate();" 
        OnClick="btnBlockUser_Click" ValidationGroup="Save" />
        <script type="text/javascript" language="javascript" >
            function validate() {
                if (Page_ClientValidate())
                return confirm('Are you sure want to Block this user ?');
            }
        </script>
Foursome answered 14/6, 2013 at 10:26 Comment(2)
Page_ClientValidate() undefined for mePinky
Summary: Page_ClientValidate() triggers the RequiredFieldValidator.Amling

© 2022 - 2024 — McMap. All rights reserved.