3

ASP:

<asp:LinkButton ID="lbSave" OnClick="lbSave_Click" runat="server">Save</asp:LinkButton>

HTML:

<a id="ContentMain_lbSave" href="javascript:__doPostBack(&#39;ctl00$ContentMain$lbSave&#39;,&#39;&#39;)">Save</a>

Code-behind:

public void lbSave_Click(object sender, EventArgs e)
    {
        if (strP != "")
        {
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "DiffUser", "showDiffUser();", true);
        }
        else
        {
            //do something else...
        }
    }

JQuery:

function showDiffUser() {
    $("#dvHide").fadeIn("slow");
    $("#backOverlay").fadeIn("slow");
}

When I click the button, I want the page to not do a postback and run the JQuery function. Instead, when I click the link it refreshes the page and then executes the JQuery function.

How can I prevent the postback.

I tried the following: onClientClick='javascript:void(0);' but that doesn't execute the JQuery function.

SearchForKnowledge
  • 3,663
  • 9
  • 49
  • 122
  • Possible duplicate of [Disable the postback on an ](https://stackoverflow.com/questions/176827/disable-the-postback-on-an-asplinkbutton) – Burgi Nov 13 '17 at 14:22

3 Answers3

3

try

OnClientClick="return false;"

UPDATE:

OnClientClick="showDiffUser();return false;"

showDiffUser(); calls the JS-Method

return false; prevents postback

fubo
  • 44,811
  • 17
  • 103
  • 137
2

You only add your jQuery function call to the page after button is clicked. Of course if you suppress the click postback the function call never gets to the page and never gets executed.

What you want though to is to call this function and then suppress, right? Therefore do this:

OnClientClick="showDiffUser(); return false;"

And you do not need the server-side click handler anymore.

Andrei
  • 55,890
  • 9
  • 87
  • 108
  • Thank you for the response. I need to have the server side call as shown in the updated question. Please advise. – SearchForKnowledge Apr 29 '15 at 14:35
  • @SearchForKnowledge, emm, a bit confused now. So do you want to disable postback completely or not? As it currently stands you want it to happen sometimes, but not always. – Andrei Apr 29 '15 at 14:37
  • So when the page loads, `strP` is assigned a value. if it is blank then execute JQuery otherwise do not execute. the `strP` is a server side variable. I know I am confusing the question. – SearchForKnowledge Apr 29 '15 at 14:40
1

If you don't want to cause a postback don't use a .Net LinkButton, use a standard HTML hyperlink instead and use jQuery to capture the event:

<a id="my-link">Save</a>

<script>
$(function () {
   $("#my-link").click(function () {
      showDiffUser();
   });
})
</script>

In the same call, you could do an AJAX call to raise your server-side validation:

<script>
$(function () {
   $("#my-link").click(function () {
      // AJAX Call
        $.ajax({
            type: "post",
            url: "your-url",
            success: function (returnData) {
                showDiffUser();
            }
        });
   });
})
</script>
Matt
  • 551
  • 5
  • 4