Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 319
y0mbo Avatar asked Oct 07 '08 00:10

y0mbo


People also ask

Does ASP NET do postback?

PostBack is the name given to the process of submitting an ASP.NET page to the server for processing. PostBack is done if certain credentials of the page are to be checked against some sources (such as verification of username and password using database).


1 Answers

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.

like image 186
Russell Myers Avatar answered Sep 19 '22 06:09

Russell Myers