简体   繁体   English

当 OnClientClick 返回 False 时,防止 LinkBut​​ton 触发 OnClick

[英]Prevent LinkButton to fire OnClick when OnClientClick returns False

i have linkbutton in my aspx page as我的aspx页面中有linkbutton

 <asp:LinkButton ID="LinkButton1" OnClientClick="if ( !MutExChkList()) return false;" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>

where on clientclick event i need to confirm something from user by using javascript and if javascript returns true then i need to fire OnClick event of button and if javascript returns false then i need to do nothing say don't postback the page...clientclick事件中,我需要使用javascript向用户确认某些内容,如果 javascript 返回 true,则我需要触发按钮的OnClick事件,如果javascript返回 false,则我不需要做任何事情,说不要postback发页面...

Below is my javascript下面是我的javascript

<script type="text/javascript">
        function MutExChkList() {
         swal({
                title: 'Are you sure?',
                text: 'You will not be able to recover this imaginary file!',
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#DD6B55',
                confirmButtonText: 'Yes, delete it!',
                cancelButtonText: 'No, cancel plx!',
                closeOnConfirm: false,
                closeOnCancel: false
            }, function (isConfirm) {
                if (isConfirm) {

                   return true;


                } else {

                    return false;
                }

            });

        };
    </script>

i got help from following links我从以下链接中得到了帮助

Stopping onclick from firing when onclientclick is false? 当 onclientclick 为假时停止 onclick 触发?

Prevent LinkButton post back OnClientClick not working. 防止 LinkBut​​ton 回发 OnClientClick 不起作用。 Why? 为什么?

and much more searching....还有更多的搜索......

if i use OnClientClick="MutExChkList(event);"如果我使用OnClientClick="MutExChkList(event);" then it postback in both cases either true or false然后它在两种情况下都回发truefalse

if i use OnClientClick="if ( !MutExChkList()) return false;"如果我使用OnClientClick="if ( !MutExChkList()) return false;" then it does not postback when function returns true那么当函数返回true时它不会postback

Can any one help me to get out of this... thanks in advance... i am using asp.net任何人都可以帮助我摆脱这种情况......在此先感谢......我正在使用asp.net

As mentioned in comment, you can't return from inside a callback and expect the outside function to even know about it.正如评论中提到的,您不能从回调内部返回并期望外部函数甚至知道它。 The function exits immediately after calling swal() and always returns undefined .该函数在调用swal()后立即退出并始终返回undefined

One option is to block the initial click and trigger the click action from code after setting a sentinel/flag.一种选择是在设置哨兵/标志后阻止初始点击并从代码触发点击操作。

Something like:就像是:

var allow = false;
function MutExChkList() {
  if (!allow) {
    swal({
      title: 'Are you sure?',
      text: 'You will not be able to recover this imaginary file!',
      type: 'warning',
      showCancelButton: true,
      confirmButtonColor: '#DD6B55',
      confirmButtonText: 'Yes, delete it!',
      cancelButtonText: 'No, cancel plx!',
      closeOnConfirm: false,
      closeOnCancel: false
    }, function(isConfirm) {
      if (isConfirm) {
        allow = true;                  // Allow next click to succeed
        $('#LinkButton1')[0].click();  // Hit the browser event direct
      }
    });
  }
  return allow;
};

The button then becomes something like:然后按钮变成这样:

<asp:LinkButton ClientIDMode="static" ID="LinkButton1" OnClientClick="return MutExChkList();" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>

Once you have a ClientID available on your button, you might as well do all the code the jQuery way:一旦您的按钮上有一个可用的ClientID ,您不妨以 jQuery 方式执行所有代码:

<asp:LinkButton ClientIDMode="static" ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>

and the jQuery:和 jQuery:

var allow = false;
$('#LinkButton1').click() {
  if (!allow) {
    swal({
      title: 'Are you sure?',
      text: 'You will not be able to recover this imaginary file!',
      type: 'warning',
      showCancelButton: true,
      confirmButtonColor: '#DD6B55',
      confirmButtonText: 'Yes, delete it!',
      cancelButtonText: 'No, cancel plx!',
      closeOnConfirm: false,
      closeOnCancel: false
    }, function(isConfirm) {
      if (isConfirm) {
        allow = true;                  // Allow next click to succeed
        $('#LinkButton1')[0].click();  // Hit the browser event direct
      }
    });
  }
  return allow;
});

I have a solution.我有一个解决方案。 yes, you can't return from inside a callback and expect the outside function to even know about it.是的,您不能从回调内部返回并期望外部函数甚至知道它。 The function exits immediately after calling swal() and always returns undefined or some return inside.该函数在调用 swal() 后立即退出并始终返回 undefined 或内部的一些返回值。

my example, I always return false, in my function I send the button object that I clicked to click on it again.在我的示例中,我总是返回 false,在我的函数中,我发送了我单击以再次单击它的按钮对象。 Then I have a variable called Myflag, my variable tells me if I clicked on the sweetalert, if Myflage is false I show the sweetalert again, if Myflag is true then I return true, OnClick immediately executes the server method.然后我有一个名为 Myflag 的变量,我的变量告诉我是否单击了 Sweetalert,如果 Myflage 为 false,则再次显示 Sweetalert,如果 Myflag 为 true,则返回 true,OnClick 立即执行服务器方法。

Something like:就像是:

 var Myflag = false; function EliminarRegistroJavaS(e) { if (Myflag == false) { Swal.fire({ title: 'Eliminar registro', text: 'Desea quitar el Registro?', icon: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', cancelButtonColor: '#d33', confirmButtonText: 'Si, Quitar!', closeOnConfirm: true }).then((result) => { if (result.value) { Swal.fire( 'Eliminado!', 'Registro de Eliminado', 'success' ); Myflag = true; e.click() }; }); return false; } else { Myflag = false; return true; } }
 <asp:LinkButton ID="lnkRemove" class="btn btn-danger btn-sm" runat="server" CommandArgument='<%# Eval("idregistro")%>' OnClientClick="return EliminarRegistroJavaS(this); return false;" Text="Borrar" OnClick="MethodoElminarServer"></asp:LinkButton>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM