Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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??

like image 689
Vishweshwar Kapse Avatar asked Jun 14 '13 09:06

Vishweshwar Kapse


People also ask

Which validator control is used to make an input control a compulsory field?

ASP.NET RequiredFieldValidator Control This validator is used to make an input control required. It will throw an error if user leaves input control empty. It is used to mandate form control required and restrict the user to provide data.

What is enable or disable required field validator in asp net?

Use ValidatorEnable function from the Asp.net javacsript Script Library to Enable/Disable the validators on client side. Sometimes you may need to enable/disable validators on client side. you can easily do this using ValidatorEnable function in the Asp.net javacsript Script Library.


1 Answers

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>
like image 100
chetanya gehlot Avatar answered Sep 27 '22 19:09

chetanya gehlot