Disable the postback on an <ASP:LinkButton>
Asked Answered
B

17

64

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?

Brewmaster answered 7/10, 2008 at 0:41 Comment(0)
A
68

ASPX code:

<asp:LinkButton ID="someID" runat="server" Text="clicky"></asp:LinkButton>

Code behind:

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

What renders as HTML is:

<a onclick="return false;" id="someID" href="javascript:__doPostBack('someID','')">clicky</a>

In this case, what happens is the onclick functionality becomes your validator. If it is false, the "href" link is not executed; however, if it is true the href will get executed. This eliminates your post back.

Around answered 7/10, 2008 at 1:21 Comment(2)
<asp:LinkButton ID="LinkButton5" runat="server" OnClick="A_Click" CssClass="linkOff" Text="Edit"></asp:LinkButton> and adding the above, when clicking nothing happens :/Spall
perfect, been struggling with this for a while as didn't want the __postback called instead called some JS. much appreciated.Sidelong
L
39

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.

Loblolly answered 7/10, 2008 at 2:5 Comment(5)
I agree. Primarily, I want to use a skin I've already created for linkbuttons used elsewhere.Brewmaster
Can you not modify the skin, or base the styles for the given anchor off those used in the skin?Loblolly
You might favor a linkbutton even if server postback is disabled when: 1) You still desire the benefit of a control implementing INamingContainer. Example, you have multiple user controls on a page where each contains a LinkButton that needs a unique ID. 2) You want to keep asp client validation.Babita
Also, sometimes LinkButtons are used as a target control for an ajax extender controlRidglee
You may want this to dynamically generate the buttons. However you can use new HtmlGenericControl()Foghorn
H
27

You can do it too

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

And it stop the link button postback

Hercule answered 1/2, 2011 at 20:6 Comment(1)
It works, but the problem is, if you have some operation going on such as deleting the record after modal pop-up, this doesn't work at all. It just simply refreshes the page. Nothing else.Chassidychassin
R
15

Just set href="#"

<asp:LinkButton ID="myLink" runat="server" href="#">Click Me</asp:LinkButton>
Radioluminescence answered 11/5, 2012 at 23:7 Comment(1)
from what i tested, this will cause the page scrolls up the top.Cuttie
H
6

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.

Hydrodynamic answered 7/10, 2008 at 2:8 Comment(0)
G
6

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;" />
Gurango answered 28/9, 2012 at 15:18 Comment(0)
N
4

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

MyButton.Attributes.Add("onclick", "put your javascript here including... return false;");
Nonrigid answered 7/10, 2008 at 0:43 Comment(0)
B
2

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.

Batting answered 16/10, 2018 at 7:16 Comment(0)
J
1

call java script function on onclick event.

Jeneejenei answered 28/9, 2010 at 12:16 Comment(0)
C
1

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

Chondriosome answered 19/10, 2010 at 16:4 Comment(0)
R
1

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" />
Richardson answered 8/11, 2013 at 16:25 Comment(0)
P
0

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

Pax answered 8/5, 2010 at 18:26 Comment(0)
M
0

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>
Matchmark answered 19/5, 2011 at 16:1 Comment(0)
F
0

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);
Fredrick answered 29/1, 2014 at 19:57 Comment(1)
Welcome to StackOverflow! Since the OP did not mention JQuery, your answer is slightly off-topic. To make the answer better, you could include additional context about how to incorporate JQuery here. On the other hand, since the question was asked and answered (many times) 5 years ago, only very on-topic answers would be useful at this point.Dagnydago
M
-1

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.

Morganica answered 7/10, 2008 at 0:51 Comment(1)
OnClick binds the server-side event, not the client side event. onclientclick is the client eventByrd
G
-1

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.

Grandiloquence answered 11/2, 2010 at 13:49 Comment(0)
P
-1

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

Pyxie answered 22/12, 2010 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.