Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RequiredFieldValidator and CausesValidation problem

Status
Not open for further replies.

vilrbn

Programmer
Oct 29, 2002
105
FR
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=&quot;BtnCancel&quot; type=&quot;button&quot; value=&quot;Cancel&quot; name=&quot;BtnCancel&quot; runat=&quot;server&quot; onserverclick=&quot;BtnCancel_ServerClick&quot;>

<asp:RequiredFieldValidator id=&quot;rfvLogin&quot; runat=&quot;server&quot; CssClass=&quot;dfont&quot; ErrorMessage=&quot;This field is mandatory.&quot; ControlToValidate=&quot;txtLogin&quot; Display=&quot;Dynamic&quot;></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(&quot;login.aspx&quot;)
End Sub
 
I'm going to make the assumtion that you're using Visual Studio.net...

Go to design view of the form...

right click on the button and then click on properties.
The 8th box down is the Causes Validation property... change it to false...

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Thanks, it's working for an ASP.NET button as CausesValidation is in the property list.

But if I right click on my HTMLInputButton, this property isn't available.
 
HTMLInputButton?? What is it your using that for?? I don't see how that would cause server validation...

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Ok, I just checked into this a little bit...

You will need to include the tag
runat=&quot;server&quot;
into the html tag...

Then on the page load event do this...

buttonname.causesvalidation = false

then it should work...

I should look at things befor I start talking:-D

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Yep ! yep ! that's working fine !
Many thanks James.
 
np boss... junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top