Disable postback at click on a button

I want do disable postback after clicking a <asp:Button>. I’ve tried to do that by assigning onclick="return false", but in the button doesn’t work.

How can I fix this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

onClientClick="return false"

That should do it! Onclick will refer to an ASP.net function, onClientClick will render as OnClick on the control in HTML.

Method 2

onclick is used to wire up your server side events. You need to use the OnClientClick handler such as <asp:button OnClientClick="return false;" />

Method 3

It worked for me like this:

In <body>:

function myfunction() {
            return false;
        }

and in <head>:

<asp:ImageButton ID="ImageButton1" runat="server" Height="52px" Width="58px" OnClientClick="myfunction(); return false;"/>

Method 4

I could fix that using an UpdatePanel which includes the implicated controls/elements.

Method 5

Use this:-

  someID.Attributes.Add("onClick", "return false;");

Method 6

Seeing as none of these answers helped me. I found a solution. Instead of using the ASP.NET button <asp:Button>, you should use the HTML button. It worked perfectly and looks exactly like the ASP.NET button. To get the HTML button to submit on server side, you use the attribute onserverclick.

<input id="btnState" class="AddButtons" runat="server" type="button" value="Add State" onclick="swap('one', 'two');" />

For my code, I was using JS to do something on the server side. The <asp:Button> would not stop doing a post back but as I said, the HTML button fixed my problem. Please mark as answer or vote up if this helped you out.

Method 7

Put your asp button to inside the Updatepanel like

<asp:UpdatePanel ID="updPnl" runat="server" UpdateMode="Conditional" RenderMode="Block">
  <ContentTemplate>
    <asp:Button ID="btnID" runat="server"  OnClick="btn_Click" />
  </ContentTemplate>
</asp:UpdatePanel>

Method 8

Since you want to do it after the postback, I presume you want to prevent double click postbacks? In this case you are best off having some sort of state maintaining variable that you set after the first click on the client side. As a simple example

var clicked = false;
function AllowOneClick(){
   if(!clicked){
      clicked = true;
      return true;
    }
 return false;
 }

You then set OnClientClick to return this method result on your button so OnclientClick="return AllowOneClick()"
This will of course only work for one button, but it should give you the general idea.

Method 9

In my case none of the solutions above worked for me.

What I wanted was to first call a function on the client side and then halt the postback to the server. My solution was to simply add the return keyword before calling my client-side function, i.e.:

<asp:Button Runat=Server OnClientClick="return fyFunction;">

My client-side script contains return false in the last line of code.

Method 10

by using UseSubmitBehavior=”false” you can disable postbacks of onclick


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x