开发者

Can I stop a post back by using javascripts confirm?

开发者 https://www.devze.com 2023-03-15 12:11 出处:网络
I have an aspx page with a asp.net button on the page: <asp:LinkButton ID=\"btn_Delete\" runat=\"server\" OnClick=\"btn_Delete_Click\" OnClientClick=\"ConfirmDelete();\" Text=\"Delete\" />

I have an aspx page with a asp.net button on the page:

<asp:LinkButton ID="btn_Delete" runat="server" OnClick="btn_Delete_Click" OnClientClick="ConfirmDelete();" Text="Delete" /> 

Confirm delete looks like this:

   function Conf开发者_开发知识库irmDelete() {
       var answer = confirm("Are you SURE you want to delete this item?");
       if (!answer) {
           return false;
       }
   }; 

I had assumed that this would prevent the page from posting back if the user clicked cancel, but it still appears to be posting back. Is there some way I can prevent postback using confirm?


You have to return the value from the function in the event handler:

OnClientClick="return ConfirmDelete();"

By the way, you don't need all that logic in the function, just return the result from the confirm call:

function ConfirmDelete() {
  return confirm("Are you SURE you want to delete this item?");
};
0

精彩评论

暂无评论...
验证码 换一张
取 消