[asp.net] Disable the postback on an <ASP:LinkButton>

I have an ASP.NET linkbutton control on my form. I would like to use it for javascript on the client side and prevent it from posting back to the server. (I'd like to use the linkbutton control so I can skin it and disable it in some cases, so a straight up tag is not preferred).

How do I prevent it from posting back to the server?

This question is related to asp.net asplinkbutton

The answer is


Just set href="#"

<asp:LinkButton ID="myLink" runat="server" href="#">Click Me</asp:LinkButton>

You can do it too

...LinkButton ID="BtnForgotPassword" runat="server" OnClientClick="ChangeText('1');return false"...

And it stop the link button postback


I think you should investigate using a HyperLink control. It's a server-side control (so you can manipulate visibility and such from code), but it omits a regular ol' anchor tag and doesn't cause a postback.


In C#, you'd do something like this:

MyButton.Attributes.Add("onclick", "put your javascript here including... return false;");

Instead of implement the attribute:

public partial class _Default : System.Web.UI.Page{
 protected void Page_Load(object sender, EventArgs e)
 {
    someID.Attributes.Add("onClick", "return false;");
 }}

Use:

OnClientClick="return false;"

inside of asp:LinkButton tag


use html link instead of asp link and you can use label in between html link for server side control


In the jquery ready function you can do something like below -

var hrefcode = $('a[id*=linkbutton]').attr('href').split(':');
var onclickcode = "javascript: if`(Condition()) {" + hrefcode[1] + ";}";
$('a[id*=linkbutton]').attr('href', onclickcode);

You might also want to have the client-side function return false.

<asp:LinkButton runat="server" id="button" Text="Click Me" OnClick="myfunction();return false;" AutoPostBack="false" />

You might also consider:

<span runat="server" id="clickableSpan" onclick="myfunction();" class="clickable">Click Me</span>

I use the clickable class to set things like pointer, color, etc. so that its appearance is similar to an anchor tag, but I don't have to worry about it getting posted back or having to do the href="javascript:void(0);" trick.


use html link instead of asp link and you can use label in between html link for server side control


Something else you can do, if you want to preserve your scroll position is this:

<asp:LinkButton runat="server" id="someId" href="javascript: void;" Text="Click Me" />

In C#, you'd do something like this:

MyButton.Attributes.Add("onclick", "put your javascript here including... return false;");

I think you should investigate using a HyperLink control. It's a server-side control (so you can manipulate visibility and such from code), but it omits a regular ol' anchor tag and doesn't cause a postback.


You might also want to have the client-side function return false.

<asp:LinkButton runat="server" id="button" Text="Click Me" OnClick="myfunction();return false;" AutoPostBack="false" />

You might also consider:

<span runat="server" id="clickableSpan" onclick="myfunction();" class="clickable">Click Me</span>

I use the clickable class to set things like pointer, color, etc. so that its appearance is similar to an anchor tag, but I don't have to worry about it getting posted back or having to do the href="javascript:void(0);" trick.


Just set href="#"

<asp:LinkButton ID="myLink" runat="server" href="#">Click Me</asp:LinkButton>

Something else you can do, if you want to preserve your scroll position is this:

<asp:LinkButton runat="server" id="someId" href="javascript: void;" Text="Click Me" />

To avoid refresh of page, if the return false is not working with asp:LinkButton use

href="javascript: void;"

or

href="#"

along with OnClientClick="return false;"

<asp:LinkButton ID="linkPrint" runat="server" CausesValidation="False" href="javascript: void;"
        OnClientClick="javascript:self.print();return false;">Print</asp:LinkButton>

Above is code will call the browser print without refresh the page.


This may sound like an unhelpful answer ... But why are you using a LinkButton for something purely client-side? Use a standard HTML anchor tag and set its onclick action to your Javascript.

If you need the server to generate the text of that link, then use an asp:Label as the content between the anchor's start and end tags.

If you need to dynamically change the script behavior based on server-side code, consider asp:Literal as a technique.

But unless you're doing server-side activity from the Click event of the LinkButton, there just doesn't seem to be much point to using it here.


To avoid refresh of page, if the return false is not working with asp:LinkButton use

href="javascript: void;"

or

href="#"

along with OnClientClick="return false;"

<asp:LinkButton ID="linkPrint" runat="server" CausesValidation="False" href="javascript: void;"
        OnClientClick="javascript:self.print();return false;">Print</asp:LinkButton>

Above is code will call the browser print without refresh the page.


You might also want to have the client-side function return false.

<asp:LinkButton runat="server" id="button" Text="Click Me" OnClick="myfunction();return false;" AutoPostBack="false" />

You might also consider:

<span runat="server" id="clickableSpan" onclick="myfunction();" class="clickable">Click Me</span>

I use the clickable class to set things like pointer, color, etc. so that its appearance is similar to an anchor tag, but I don't have to worry about it getting posted back or having to do the href="javascript:void(0);" trick.


You can do it too

...LinkButton ID="BtnForgotPassword" runat="server" OnClientClick="ChangeText('1');return false"...

And it stop the link button postback


Just been through this, the correct way to do it is to use:

  1. OnClientClick
  2. return false

as in the following example line of code:

<asp:LinkButton ID="lbtnNext" runat="server" OnClientClick="findAllOccurences(); return false;" />

Have you tried to use the OnClientClick?

var myLinkButton = new LinkButton { Text = "Click Here", OnClientClick = "JavaScript: return false;" };

<asp:LinkButton ID="someID" runat="server" Text="clicky" OnClientClick="JavaScript: return false;"></asp:LinkButton>

This may sound like an unhelpful answer ... But why are you using a LinkButton for something purely client-side? Use a standard HTML anchor tag and set its onclick action to your Javascript.

If you need the server to generate the text of that link, then use an asp:Label as the content between the anchor's start and end tags.

If you need to dynamically change the script behavior based on server-side code, consider asp:Literal as a technique.

But unless you're doing server-side activity from the Click event of the LinkButton, there just doesn't seem to be much point to using it here.


Just been through this, the correct way to do it is to use:

  1. OnClientClick
  2. return false

as in the following example line of code:

<asp:LinkButton ID="lbtnNext" runat="server" OnClientClick="findAllOccurences(); return false;" />

In C#, you'd do something like this:

MyButton.Attributes.Add("onclick", "put your javascript here including... return false;");

Have you tried to use the OnClientClick?

var myLinkButton = new LinkButton { Text = "Click Here", OnClientClick = "JavaScript: return false;" };

<asp:LinkButton ID="someID" runat="server" Text="clicky" OnClientClick="JavaScript: return false;"></asp:LinkButton>

No one seems to be doing it like this:

createEventLinkButton.Attributes.Add("onClick", " if (this.innerHTML == 'Please Wait') { return false; } else {  this.innerHTML='Please Wait'; }");

This seems to be the only way that works.


call java script function on onclick event.


Why not use an empty ajax update panel and wire the linkbutton's click event to it? This way only the update panel will get updated, thus avoiding a postback and allowing you to run your javascript


In the jquery ready function you can do something like below -

var hrefcode = $('a[id*=linkbutton]').attr('href').split(':');
var onclickcode = "javascript: if`(Condition()) {" + hrefcode[1] + ";}";
$('a[id*=linkbutton]').attr('href', onclickcode);

You might also want to have the client-side function return false.

<asp:LinkButton runat="server" id="button" Text="Click Me" OnClick="myfunction();return false;" AutoPostBack="false" />

You might also consider:

<span runat="server" id="clickableSpan" onclick="myfunction();" class="clickable">Click Me</span>

I use the clickable class to set things like pointer, color, etc. so that its appearance is similar to an anchor tag, but I don't have to worry about it getting posted back or having to do the href="javascript:void(0);" trick.


Instead of implement the attribute:

public partial class _Default : System.Web.UI.Page{
 protected void Page_Load(object sender, EventArgs e)
 {
    someID.Attributes.Add("onClick", "return false;");
 }}

Use:

OnClientClick="return false;"

inside of asp:LinkButton tag


No one seems to be doing it like this:

createEventLinkButton.Attributes.Add("onClick", " if (this.innerHTML == 'Please Wait') { return false; } else {  this.innerHTML='Please Wait'; }");

This seems to be the only way that works.


I think you should investigate using a HyperLink control. It's a server-side control (so you can manipulate visibility and such from code), but it omits a regular ol' anchor tag and doesn't cause a postback.


This may sound like an unhelpful answer ... But why are you using a LinkButton for something purely client-side? Use a standard HTML anchor tag and set its onclick action to your Javascript.

If you need the server to generate the text of that link, then use an asp:Label as the content between the anchor's start and end tags.

If you need to dynamically change the script behavior based on server-side code, consider asp:Literal as a technique.

But unless you're doing server-side activity from the Click event of the LinkButton, there just doesn't seem to be much point to using it here.


Why not use an empty ajax update panel and wire the linkbutton's click event to it? This way only the update panel will get updated, thus avoiding a postback and allowing you to run your javascript