0

Ajax Post URL not working on ASP.net Image Button OnClientClick . I want to get the value through Ajax Post URL in OnClientClick.

 <asp:ImageButton ID="btngo" runat="server" ImageUrl="~/oea_images/login_btn.png"
      OnClick="btngo_Click" OnClientClick="clean_all();" Width="80px" />

    function clean_all() {
        try {
            alert("enter");
            $.ajax({
                type: "POST",
                url: "WebForm9.aspx/ForceOut",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msgs) {
                    //alert(msgs);
                    //alert(msgs.d);
                    if (msgs.d == "out") {
                        alert("check");
                    }
                }
            });
        } catch (ex) {
            alert(ex.Message);
        }
    }
Bharathi
  • 1,415
  • 3
  • 15
  • 21

2 Answers2

2

return false to avoid postback. Also remove the OnClick attribute of the asp Image button and give a try.

       var varglobal=false;
                function clean_all() {
                        try {
                          if(!varglobal){
                            alert("enter");
                            $.ajax({
                                type: "POST",
                                url: "WebForm9.aspx/ForceOut",
                                data: "{}",
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                success: function (msgs) {
                                    //alert(msgs);
                                    //alert(msgs.d);
                                    if (msgs.d == "out") {
                                        alert("check");

                                        varglobal=true;
                                        $("#btngo").click();// hopes master page isn't                           used.

                                    }
                                }
                            });
                          }
                        } catch (ex) {
                            alert(ex.Message);
                        }
                     return varglobal;
                    }

Since it's an ASP Image button, the return false maynot prevent the post back(The postback is not a default function of input[type=image]). Refer here to know it how.

When a postback happens, the Ajax request to server will get cancelled and error function will be executed. You can avoid this by preventing the postback.

Community
  • 1
  • 1
Subin Jacob
  • 4,692
  • 10
  • 37
  • 69
0

If you use this way, you can avoid postback, but you can't call the server function after that directly. You have to postback at success callback.

You can use simple anchor tag, asp:imagebutton not necessary.

<asp:ImageButton ID="btngo" runat="server" ImageUrl="~/oea_images/login_btn.png"
  OnClientClick="return clean_all();" Width="80px" />

function clean_all() {
    try {
        alert("enter");
        $.ajax({
            type: "POST",
            url: "WebForm9.aspx/ForceOut",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msgs) {                   
                if (msgs.d == "out") {                     
                    //call btngo_Click here with __doPostback()
                    // or execute the code inside "btngo_Click"
                    // on server after "ForceOut" completed
                }
            }
        });
    } catch (ex) {
        alert(ex.Message);
    }
return false;
}
speti43
  • 2,886
  • 1
  • 20
  • 23