Hello,
I have 4 textbox fields (Login, Current Pwd, New Pwd, Confirm Pwd) and 2 HTMLInputButtons: Ok, Cancel.
I put a RequiredFieldValidator field on each Textbox to ensure that they will not be empty.
My problem: When I click on Cancel (and all my fields are empty), the validation is processed before entering in my ServerClick function. My ServerClick function is never launched as the page is not validated.
I tried with an <asp:button> but had the same problem.
How do I have to use the CausesValidation property ?
An extract of Microsoft doc:
You can specify or determine whether validation is performed on both the client and the server when an HtmlInputButton control is clicked by using the CausesValidation property. To prevent validation from being performed, set the CausesValidation property to false.
This property is commonly used in the event handler for the ServerClick event to prevent page validation from occurring when a Cancel or Reset button is clicked.
ASPX code:
<INPUT id="BtnCancel" type="button" value="Cancel" name="BtnCancel" runat="server" onserverclick="BtnCancel_ServerClick">
<asp:RequiredFieldValidator id="rfvLogin" runat="server" CssClass="dfont" ErrorMessage="This field is mandatory." ControlToValidate="txtLogin" Display="Dynamic"></asp:RequiredFieldValidator>
Code behind:
Public Sub BtnCancel_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.ServerClick
BtnCancel.CausesValidation = False
Response.Redirect("login.aspx"
End Sub
I have 4 textbox fields (Login, Current Pwd, New Pwd, Confirm Pwd) and 2 HTMLInputButtons: Ok, Cancel.
I put a RequiredFieldValidator field on each Textbox to ensure that they will not be empty.
My problem: When I click on Cancel (and all my fields are empty), the validation is processed before entering in my ServerClick function. My ServerClick function is never launched as the page is not validated.
I tried with an <asp:button> but had the same problem.
How do I have to use the CausesValidation property ?
An extract of Microsoft doc:
You can specify or determine whether validation is performed on both the client and the server when an HtmlInputButton control is clicked by using the CausesValidation property. To prevent validation from being performed, set the CausesValidation property to false.
This property is commonly used in the event handler for the ServerClick event to prevent page validation from occurring when a Cancel or Reset button is clicked.
ASPX code:
<INPUT id="BtnCancel" type="button" value="Cancel" name="BtnCancel" runat="server" onserverclick="BtnCancel_ServerClick">
<asp:RequiredFieldValidator id="rfvLogin" runat="server" CssClass="dfont" ErrorMessage="This field is mandatory." ControlToValidate="txtLogin" Display="Dynamic"></asp:RequiredFieldValidator>
Code behind:
Public Sub BtnCancel_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.ServerClick
BtnCancel.CausesValidation = False
Response.Redirect("login.aspx"
End Sub