views:

13827

answers:

10

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?

+1  A: 

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

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

BoltBait
A: 

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.

tvanfosson
OnClick binds the server-side event, not the client side event. onclientclick is the client event
Glenn Slaven
+8  A: 

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.

Russell Myers
+3  A: 

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.

John Rudy
I agree. Primarily, I want to use a skin I've already created for linkbuttons used elsewhere.
y0mbo
Can you not modify the skin, or base the styles for the given anchor off those used in the skin?
John Rudy
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.
eniac
+2  A: 

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.

John Sheehan
A: 

GENIAL las Respuestas. Thanks a lot!!!

Pablo
A: 

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.

Peter
A: 

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

MisterMarc
A: 

call java script function on onclick event.

Rizwan Majeed
A: 

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

Jaider