Reputation: 1387
I have a master page that contains a button with a PostBackUrl. I add validation to the button using jquery and return false if the form is not valid.
The button:
<asp:Button ID="btnSearch" runat="server" Text="Search" CssClass="search-button"
PostBackUrl="/some/rewriten/url" />
The javascript:
<script type="text/javascript">
$(function () {
$(".search-button").click(function (e) {
if (!ValidateForm()) {
return false;
//e.preventDefault();
}
});
});
</script>
I also have another page with a form where users can leave contact information. This form has a normal button:
<asp:Button ID="btnSend" runat="server" Text="Send"
CssClass="button" OnClick="btnSend_Click" />
If i use the first button and the form is not valid, error messages are printed out and the user is not redirected to the PostBackUrl page. After this if i click the other form's btnSend button, the user is redirected to the PostBackUrl page, as if the post back url is set and saved from before.
Any idea why this is happening, and how to solve this problem?
I read somewhere that doing this OnClientClick="if(!ValidateForm())? return false;"
would probably solve the issue, but adding the OnClientClick is unacceptable in my scenario, the click handler for validation has to be added by javascript.
Upvotes: 2
Views: 1048