4

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

5
  • Page is doing postback means your javascript code is wrong. Have u tried to find it out where u r getting error??? Nov 22, 2013 at 11:55
  • 1
    yes when UseSubmitBehavior="false" is added to asp.net button then _doPostback() is automatically added to button. Nov 22, 2013 at 12:01
  • 1
    Do you really need to have UseSubmitBehavior? You can get rid of that and explicitly define OnClick. OnClick won't fire unless OnClientClick returns true.
    – Abhitalks
    Nov 22, 2013 at 12:04
  • return LoginPopup(); try this instead OnClientClick="if (!LoginPopup()) {return false;}"
    – Just code
    Nov 23, 2013 at 3:32
  • 3
    still it does postback. Nov 24, 2013 at 10:42

2 Answers 2

1

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> 
0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.