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
Thank u it helped me to solve my problem
ReplyDeleteNice to hear that..
Delete+1 Thank's alot u've saved my day
ReplyDeleteHi,
DeleteNice to hear that.
Thanks a lot.really i was stuck in that...
ReplyDelete...
:)
thanks, this really helped me out :)
ReplyDeleteBriliant... Thanks for the help
ReplyDeleteThat helped me out a lot! Cheers!
ReplyDeletemuch appreciated, getting strange behaviour without this
ReplyDeletethxxxxxxxxxxx
ReplyDeleteHi this is very helpful! Thanks for this :)
ReplyDeleteThanks 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?
ReplyDeleteFor 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"
ReplyDeleteThis solved my issue;
DeleteUseSubmitBehavior="false"
Thank you.
This is exactly what i was looking for, thank you.
ReplyDeleteThank you very much.This article is very helpful.
ReplyDeleteI love you =)
ReplyDeleteThere 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!
ReplyDeleteCool and that i have a super supply: How To Renovate House Exterior 1970's split level remodel
ReplyDelete