Disable postback at click on a button

2019-02-11 21:30发布

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?

10条回答
在下西门庆
2楼-- · 2019-02-11 21:58

Use this:-

  someID.Attributes.Add("onClick", "return false;");
查看更多
做自己的国王
3楼-- · 2019-02-11 22:01

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.

查看更多
孤傲高冷的网名
4楼-- · 2019-02-11 22:06
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.

查看更多
可以哭但决不认输i
5楼-- · 2019-02-11 22:10

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.

查看更多
我命由我不由天
6楼-- · 2019-02-11 22:12

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

查看更多
狗以群分
7楼-- · 2019-02-11 22:17

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

查看更多
登录 后发表回答