WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="sweetalert.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script> <script> function sweetAlertConfirm(btnDelete, name) { if (btnDelete.dataset.confirmed) { btnDelete.dataset.confirmed = false; return true; } else { event.preventDefault(); swal({ title: "Are you sure you want to delete " + name + " ?", text: "Once deleted, you will not be able to recover it!", icon: "warning", buttons: true, dangerMode: true, }) .then((willDelete) => { if (willDelete) { btnDelete.dataset.confirmed = true; btnDelete.click(); //swal("Deleted", "The data is deleted!", "success"); } else { //swal("The data is safe!"); } }); } } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:ListView ID="ListView1" runat="server" OnItemCommand="ListView1_ItemCommand" OnItemDeleting="ListView1_ItemDeleting"> <LayoutTemplate> <table class="table table-bordered table-striped"> <tr class="bg-danger text-white"> <th style=" 50px">ID</th> <th style=" 50px">Name</th> <th style=" 70px">Delete</th> </tr> <tbody> <asp:PlaceHolder ID="itemPlaceHolder" runat="server" /> </tbody> </table> </LayoutTemplate> <ItemTemplate> <tr> <td style="text-align: center"><%# Eval("Id")%></td> <td style="text-align: center"> <asp:Label runat="server" ID="lb" Text='<%#Eval("Name") %>'></asp:Label> </td> <td style="text-align: center"> <asp:LinkButton runat="server" ID="btnDelete" CommandName="Delete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id") %>' <%--传值到js--%> OnClientClick='<%# Eval("Name","return sweetAlertConfirm(this, "{0}");") %>'> Delete </asp:LinkButton> </td> </tr> </ItemTemplate> </asp:ListView> </div> </form> </body> </html>
WebForm1.aspx.cs
public partial class WebForm1 : System.Web.UI.Page { List<Student> allMyDataStuffs; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { allMyDataStuffs = new List<Student>() { new Student{ Id="001", Name ="A"}, new Student{ Id="002", Name ="B"}, new Student{ Id="003", Name ="C"}, new Student{ Id="004", Name ="D"}, new Student{ Id="005", Name ="E"}, }; ListView1.DataSource = allMyDataStuffs; ListView1.DataBind(); } } protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e) { if (e.CommandName == "Delete") { e.Item.Visible = false; } } protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e) { // TODO } } public class Student { public String Id { get; set; } public String Name { get; set; } }
测试结果: