Friday, December 11, 2009

Weird, CausesValidation not working.

Recently I faced a weird situation where I had some validation controls, a cancel and submit buttons in a page . Submit button would validate the page controls for errors and cancel button would just postback and execute some code on the server without validating the page. The weird thing was once submit button is clicked and if there are any validation errors in the page and then one clicks the cancel button it won’t clear postback. If the user clicks the cancel button for the second time it would trigger postback. This happened even after setting the “CausesValidation” property of the cancel button to “false'”. The cancel button code is pasted below.

<asp:Button ID="btnRemove" CssClass="buttonEnabled" CommandName="Delete" runat="server"                       meta:resourcekey="btnRemove" CausesValidation="false" />

One thing to note here is that the cancel button was inside a ListView control’ ItemTemplate section. So on click of the button  postback should happen and in the server some logic would be carried. The cancel would not work only when the user clicks the submit button and if there are any error and the user wishes to cancel the action then the cancel button would not post back. Only when the user clicks the cancel button for the second time, it would post back. If the user clicks the cancel button immediately after clicking the submit button and there are any validation errors displayed through the validation controls then the cancel button would not postback. This was weird. The solution was also simple. I need to set “Page_ValidationActive” javascript variable to false on the “OnClientClick” property of the cancel button. The modified cancel button HTML is pasted below.

<asp:Button ID="btnRemove" CssClass="buttonEnabled" CommandName="Delete" runat="server"                       meta:resourcekey="btnRemove" CausesValidation="false" OnClientClick="javascript:Page_ValidationActive = false;" />

Setting the “Page_ValidationActive” to false disables all the validations in the page. So please do keep this in mind. Anyway this is what I wanted and my problem was solved.

Try to know more.

Sandeep

18 comments:

  1. Thank u it helped me to solve my problem

    ReplyDelete
  2. +1 Thank's alot u've saved my day

    ReplyDelete
  3. Thanks a lot.really i was stuck in that...

    ...
    :)

    ReplyDelete
  4. thanks, this really helped me out :)

    ReplyDelete
  5. Briliant... Thanks for the help

    ReplyDelete
  6. That helped me out a lot! Cheers!

    ReplyDelete
  7. much appreciated, getting strange behaviour without this

    ReplyDelete
  8. Hi this is very helpful! Thanks for this :)

    ReplyDelete
  9. Thanks a lot.. so we will have to add this for all controls those are having a postback and do not need validation? Any solution to generalize this?

    ReplyDelete
  10. For me that problem was present when the button have a server side event(Onclick) attached to it. In that case you have to tell the object to not behiave like a submit button with: UseSubmitBehavior="false"

    ReplyDelete
    Replies
    1. This solved my issue;
      UseSubmitBehavior="false"
      Thank you.

      Delete
  11. This is exactly what i was looking for, thank you.

    ReplyDelete
  12. Thank you very much.This article is very helpful.

    ReplyDelete
  13. There Are Many Complaints About XM REVIEW Broker In The Internet But You Should Read This Review Before Investing Your Money With Them. We Have Personally Tested XM Fx And Found It To Be A Scam, Avoid Them At All Costs!

    ReplyDelete

Please provide your valuable comments.