24

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?

Yuck
  • 49,664
  • 13
  • 105
  • 135
  • To clarify, do you want only client side processing for the asp:button? – arviman Sep 25 '11 at 19:24
  • For those who stuck like me with this problem. The Missing attribute `type="button"` was the culprit in my case. https://stackoverflow.com/a/17995381/1177964 – Fedor Jan 20 '22 at 08:53

10 Answers10

19
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.

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
  • Hello, assigning this attribute, the ASP.NET function doesn't work. –  Sep 25 '11 at 20:21
  • what's happening is that asp.net by default reverts to the browser's submit mechanism, which you've just overwritten with your client side script. – arviman Sep 25 '11 at 20:59
4

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

arviman
  • 5,087
  • 41
  • 48
  • Hello, as I've explained to Tom, assigning this attribute, the ASP.NET function doesn't work. –  Sep 25 '11 at 20:25
  • 3
    try doing `` When you make `UseSubmitBehavior` true, asp.net will add the necessary client-side script to post the submit to the server. – arviman Sep 25 '11 at 20:50
  • Hi! I've tried doing that but using that, the button doesn't work. Remember that the button does a C# (ASP.NET) function which is display some info in a textbox (additional info). –  Sep 25 '11 at 21:28
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;"/>
Ionut
  • 724
  • 2
  • 9
  • 25
3

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

Devin Burke
  • 13,642
  • 12
  • 55
  • 82
  • 1
    Note that this does not disable post backs. It merely uses AJAX to submit the post... – Devin Burke Dec 12 '11 at 01:43
  • 1
    Yes, I had known that. **Using an UPDATEPANEL, the postback occurs** under this **without recharge the page** and I know that to disable a postback, **I've to use the AutoPostBack property**. –  Dec 16 '11 at 20:02
2

Use this:-

  someID.Attributes.Add("onClick", "return false;");
Sunil Chaudhary
  • 397
  • 1
  • 9
  • 31
1

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
bds89
  • 165
  • 3
  • 19
1

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>
Musa Jahun
  • 66
  • 1
  • 6
0

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.

Hugo Nava Kopp
  • 2,906
  • 2
  • 23
  • 41
0

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

seetha
  • 199
  • 2
  • 7
0

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.

kmcc049
  • 2,783
  • 17
  • 13
  • Hello, first thanks for your help but this answer doesn't fix my problem but the app doesn't change on your processing –  Sep 25 '11 at 20:23
  • ok I think you need to reword your question. What you want is for the button to post back to the server do some processing and then to return to the browser disabled so that it cant be clicked after it has processed server side? If that is the case on the server side set the Enabled property of the button to false. – kmcc049 Sep 25 '11 at 21:43
  • Hello, I want that **my button display a value assigned to a dropdown list item in a textbox without recharge the page displaying things as a superbox newly**. –  Sep 26 '11 at 15:12
  • So your question is actually something like 'How do I use javascript to perform this server side function on the client?' – kmcc049 Sep 26 '11 at 19:55
  • Hello, **I ONLY WANT THAT THE PAGE WON'T BE RELOADED (ALSO JS CODE) AFTER CLICK ON THE ASP:BUTTON** –  Sep 27 '11 at 13:23