Reputation: 27996
I have an asp.net button with UseSubmitBehavior="false" like this:
<asp:Button ID="btnLogin" runat="server" UseSubmitBehavior="false" OnClientClick="if (!LoginPopup()) {return false;}" CssClass="login-button" Text="<%$ Resources: HRGELoggedOutMaster, Login %>"> </asp:Button>
and js function like this:
function LoginPopup() {
var username = $("#<%= UsernameTextBox.ClientID %>").val();
var password = $("#<%= PasswordTextBox.ClientID %>").val();
var remMe;
if ($('#<%= chkRememberMe.ClientID %>').attr('checked') == "checked") {
remMe = true;
}
else {
remMe = false;
}
if (username != '' && password != '')
CallPageMethod("LoginPopup", ["username", username, "password", password, "rememberMe", remMe], LoginPopupSucceeded, LoginPopupFailed);
else
alert("Invalid username/password");
return false;
}
and I call it on keypress of textboxes like this:
$("#<%=this.UsernameTextBox.ClientID%>").keypress(function (event) {
fireButtonClick($("#<%=this.btnLogin.ClientID%>"), event);
});
function fireButtonClick(ctrl, event) {
if (event.which || event.keyCode) {
if ((event.which == 13) || (event.keyCode == 13)) {
ctrl.click();
return false;
}
I want to stop postback when input is invalid but It is not working. Please suggest me how to stop postback
Upvotes: 4
Views: 9401
Reputation: 87
When UseSubmitBehavior="false" is added to ASP.NET button then __doPostback('btnLogin','') is automatically added to onclick event after your OnClientClick definition, so avoid __doPostback side server you must add return like that
<asp:Button ID="btnLogin" runat="server" UseSubmitBehavior="false"
OnClientClick="if (!LoginPopup()) {return false;} return;" CssClass="login-button" Text="<%$ Resources: HRGELoggedOutMaster, Login %>"> </asp:Button>
Upvotes: 0
Reputation: 3794
I don't know if your problem is the same I have. Inside updatepanel when i put UseSubmitBehavior=false the page makes postback.
So I put one defaultbutton in panel, like:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" defaultbutton="btnDisableEnter">
<ContentTemplate>
<asp:Button ID="btnDisableEnter" runat="server" Text="fake button" OnClientClick="return false;" style="display:none;"/>
</ContentTemplate></asp:UpdatePanel>
Upvotes: 1