繁体   English   中英

如何使用Sweet Alert替换标准确认对话框?

[英]How to use Sweet Alert to replace standard confirm dialog?

我要替换标准的确认对话框。 我的javascript函数在MasterPage中。

function checkDelete() {
  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!", 
    closeOnConfirm: false 
  },function () { 
      swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
  });
}

我想在内容页面中调用checkDelete函数。如何在gridview templatefield按钮中使用甜蜜警报?

<asp:TemplateField ItemStyle-Width="60" HeaderText="Delete">     
  <ItemTemplate>        
    <asp:LinkButton ID="lbDelete" Runat="server" OnClientClick=" return confirm('Are you sure?');" CommandName="Delete">Delete</asp:LinkButton>     
  </ItemTemplate>
</asp:TemplateField>
</Columns>

请帮忙。

由于javascript在母版页中可用,因此它在当前页中同样可用(如果使用相同的母版)。 如下更改OnClientClick事件,它应该可以工作。

<asp:LinkButton ID="lbDelete" Runat="server" OnClientClick="checkDelete();" CommandName="Delete">Delete</asp:LinkButton>

更新资料

这就是我模拟您的项目所做的。

  • 创建了一个新的Web应用程序项目
  • Scripts文件夹中添加了甜蜜的警报脚本,css文件
  • </head>之前添加了HTML之类的参考

<link rel="stylesheet" href="Scripts/sweetalert.css"> <script src="Scripts/sweetalert.min.js"></script>

  • 添加了与</body>之前在主文件中提供的功能相同的javascript函数
  • 在default.aspx代码中添加了以下几行

<asp:LinkButton ID="lbDelete" Runat="server" OnClientClick="checkDelete();return false;" CommandName="Delete">Delete</asp:LinkButton>

这可以正确显示甜蜜警报。

在此处输入图片说明

暂无
暂无

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

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