title: SweetAlert confirmation dialog with asp.net listview delete?
body: <p>Please help me fathom this.</p>
  
  <p>I have created a ListView, displaying data from a SQL database. I have enabled inserting, editing and deleting and it all works.</p>
  
  <p><strong>What do I want?</strong></p>
  
  <p>I want to use <a href="http://t4t5.github.io/sweetalert/" rel="nofollow"><code>SweetAlert</code></a> to prompt the user to confirm <strong>yes</strong>/<strong>no</strong> whether they want to delete the entry from the ListView or not.</p>
  
  <p><strong>What have I done?</strong> </p>
  
  <p><strong><em>First</em></strong> I tried using the "builtin" functionality where I added <code>OnClientClick="return confirm('are you sure')"</code> to the <code>&lt;asp:Button/&gt;</code> calling the delete of the given ListView entry. That worked! When I clicked yes it deleted and no it didn't. I didn't have to do anything other than add the above. <strong>But</strong> It is not what I want. I want the fancier <code>SweetAlert</code> to be displayed, and here the problem starts.</p>
  
  <p><strong><em>Second</em></strong> I thought I could simply create the <code>SweetAlert</code> script and call its function name from the button. However when doing so, it does open <code>SweetAlert</code> but before I even get the chance to click yes and no, it has already deleted the item and closes the box.</p>
  
  <pre><code>&lt;script&gt;
      function deletealert()
      {
          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) {
                  swal("Deleted!", "Your imaginary file has been deleted.", "success");
              } else {
                  swal("Cancelled", "Your imaginary file is safe :)", "error");
              }
          });
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>Now I know that there is no functionality in the above, but I didn't even get the chance to move to the yes and no, it closed the script by itself. Then I found out that I could stop the deleting by setting <code>CausesValidation=false</code> on the delete <code>&lt;asp:Button /&gt;</code> but then nothing happened at all.</p>
  
  <p><strong><em>Third</em></strong> I think I have a breakthrough, but I have no clue how to finish it. I found out that on the ListView, there is an event called <strong>ItemDeleting</strong>. This event fires before the delete is executed. I tested it out, and it works.</p>
  
  <pre><code>protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
      {
          ClientScript.RegisterStartupScript(GetType(), "hwa", "deletealert();", true); //Calls the sweetalert
  
          e.Cancel = true;
         //e.Cancel = false;
      }
  </code></pre>
  
  <p>If I use the <code>e.Cancel = true;</code> then the item is not deleted and the action is cancelled. If I use the <code>e.Cancel = false;</code> then the item is deleted. So I think I may have to incorporate that functionality with the above jQuery. I don't know if I can put jQuery inside the protected void and work with it from there either?</p>
  
  <p><strong><em>Updated to include suggested solution from haraman</em></strong> Here is also the entire .aspx page:</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="forum-front.aspx.cs" Inherits="initial.site.forum_front" EnableViewState="true" EnableEventValidation="true" %&gt;
  
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"&gt;
  
  &lt;link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"&gt;
  &lt;script src="http://code.jquery.com/jquery-1.9.1.js"&gt;&lt;/script&gt;
  &lt;script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"&gt;&lt;/script&gt;
  &lt;link rel="stylesheet" href="/resources/demos/style.css"&gt;
  &lt;script src="Content/sweetalert.min.js"&gt;&lt;/script&gt;
  
  &lt;%--CSS Style Sheets--%&gt;
    &lt;link href="Content/Styles.css" rel="stylesheet" /&gt;
    &lt;link href="Content/StylesPanel.css" rel="stylesheet" /&gt;
    &lt;link href="Content/sweetalert.css" rel="stylesheet" /&gt;
  
    &lt;%--Java Scripts--%&gt;
      &lt;script&gt;
        function deletealert(ctl) {
          // STORE HREF ATTRIBUTE OF LINK CTL (THIS) BUTTON
          var defaultAction = $(ctl).prop("href");
          // CANCEL DEFAULT LINK BEHAVIOUR
          event.preventDefault();
          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) {
              swal("Deleted!", "Your imaginary file has been deleted.", "success");
              // RESUME THE DEFAULT LINK ACTION
              eval(defaultAction);
              return true;
            } else {
              swal("Cancelled", "Your imaginary file is safe :)", "error");
              return false;
            }
          });
        }
      &lt;/script&gt;
  
      &lt;asp:Panel ID="Panel1" runat="server" Height="1401px"&gt;
        &lt;center&gt;
          &lt;table&gt;
            &lt;tr&gt;
              &lt;td&gt;
                &lt;asp:Button ID="TilForsiden" runat="server" OnClick="TilForsiden_Click" Text="Forsiden" CssClass="button" /&gt;
              &lt;/td&gt;
              &lt;td&gt;
                &lt;asp:Panel ID="Panel2" runat="server" CssClass="panel panel-default"&gt;
                  &lt;h1&gt;&lt;asp:Label ID="ForumOverskrift" runat="server" CssClass=""&gt;&lt;/asp:Label&gt;&lt;/h1&gt;
                &lt;/asp:Panel&gt;
              &lt;/td&gt;
            &lt;/tr&gt;
          &lt;/table&gt;
        &lt;/center&gt;
  
        &lt;center&gt;
          &lt;asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" InsertItemPosition="LastItem" DataKeyNames="OpslagsID" OnDataBound="SkrivOpslag_Click"&gt;
  
            &lt;AlternatingItemTemplate&gt;
              &lt;tr style=""&gt;
                &lt;td&gt;
                  &lt;asp:LinkButton OnClientClick="return deletealert(this);" ID="LinkButton1" runat="server" CommandName="Delete" Text="Slet" CssClass="btn btn-default btn-xs" Visible='&lt;%# (string)Eval("BrugerNavn") == "testuser" ? true : false %&gt;' /&gt;
                  &lt;asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Rediger" CssClass="btn btn-default btn-xs" Visible='&lt;%# (string)Eval("BrugerNavn") == "testuser" ? true : false %&gt;' /&gt;
                  &lt;asp:Button ID="AnswerButton" runat="server" CommandName="Answer" Text="Svar" CssClass="btn btn-default btn-xs" OnClick="AnswerButton_Click" /&gt;
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:Label ID="IndholdLabel" runat="server" Text='&lt;%# Eval("Indhold") %&gt;' /&gt;
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:Label ID="BrugerNavnLabel" runat="server" Text='&lt;%# Eval("BrugerNavn") %&gt;' /&gt;
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:Label ID="PostnummerLabel" runat="server" Text='&lt;%# Eval("Postnummer") %&gt;' /&gt;
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:Label ID="EmneLabel" runat="server" Text='&lt;%# Eval("Emne") %&gt;' /&gt;
                &lt;/td&gt;
  
              &lt;/tr&gt;
              &lt;tr&gt;
                &lt;td&gt;&lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:TextBox ID="AnswerTextBox" Placeholder="Svar..." runat="server" CssClass="form-control" ToolTip="Skriv dit emne her" Width="500px" Visible="false" TextMode="MultiLine" Rows="3" /&gt;
                &lt;/td&gt;
              &lt;/tr&gt;
            &lt;/AlternatingItemTemplate&gt;
  
            &lt;EditItemTemplate&gt;
              &lt;tr style=""&gt;
                &lt;td&gt;
                  &lt;asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" CssClass="btn-info" /&gt;
                  &lt;asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" CssClass="btn-default" /&gt;
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:TextBox ID="IndholdTextBox" runat="server" Text='&lt;%# Bind("Indhold") %&gt;' /&gt;
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:TextBox ID="EmneTextBox" runat="server" Text='&lt;%# Bind("Emne") %&gt;' /&gt;
                &lt;/td&gt;
              &lt;/tr&gt;
            &lt;/EditItemTemplate&gt;
  
            &lt;EmptyDataTemplate&gt;
              &lt;table runat="server" style=""&gt;
                &lt;tr&gt;
                  &lt;td&gt;No data was returned.&lt;/td&gt;
                &lt;/tr&gt;
              &lt;/table&gt;
            &lt;/EmptyDataTemplate&gt;
  
            &lt;InsertItemTemplate&gt;
  
              &lt;table&gt;
                &lt;tr&gt;
                  &lt;td&gt;
                    &lt;asp:TextBox ID="EmneTextBox" Placeholder="Emne..." runat="server" Text='&lt;%# Bind("Emne") %&gt;' CssClass="form-control" ToolTip="Skriv dit emne her" Width="500px" /&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td&gt;
                    &lt;asp:TextBox ID="IndholdTextBox" Placeholder="Skriv her..." runat="server" Text='&lt;%# Bind("Indhold") %&gt;' CssClass="form-control" ToolTip="Skriv dit indhold her" TextMode="MultiLine" Rows="8" Width="500px" /&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
              &lt;/table&gt;
  
              &lt;tr style=""&gt;
                &lt;td&gt;
                  &lt;asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Udgiv" CssClass="btn-info" /&gt;
                  &lt;asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Ryd" CssClass="btn-default" /&gt;
                &lt;/td&gt;
                &lt;td&gt;&lt;/td&gt;
  
              &lt;/tr&gt;
            &lt;/InsertItemTemplate&gt;
  
            &lt;ItemTemplate&gt;
              &lt;tr style=""&gt;
                &lt;td&gt;
                  &lt;asp:LinkButton OnClientClick="return deletealert(this);" ID="LinkButton2" runat="server" CommandName="Delete" Text="Slet" CssClass="btn btn-default btn-xs" Visible='&lt;%# (string)Eval("BrugerNavn") == "testuser" ? true : false %&gt;' /&gt;
                  &lt;asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" CssClass="btn btn-default btn-xs" Visible='&lt;%# (string)Eval("BrugerNavn") == "testuser" ? true : false %&gt;' /&gt;
                  &lt;asp:Button ID="AnswerButton" runat="server" CommandName="Answer" Text="Svar" CssClass="btn btn-default btn-xs" OnClick="AnswerButton_Click" /&gt;
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:Label ID="IndholdLabel" runat="server" Text='&lt;%# Eval("Indhold") %&gt;' /&gt;
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:Label ID="BrugerNavnLabel" runat="server" Text='&lt;%# Eval("BrugerNavn") %&gt;' /&gt;
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:Label ID="PostnummerLabel" runat="server" Text='&lt;%# Eval("Postnummer") %&gt;' /&gt;
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:Label ID="EmneLabel" runat="server" Text='&lt;%# Eval("Emne") %&gt;' /&gt;
                &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                &lt;td&gt;&lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:TextBox ID="AnswerTextBox" Placeholder="Svar..." runat="server" CssClass="form-control" ToolTip="Skriv dit emne her" Width="500px" Visible="false" TextMode="MultiLine" Rows="3" /&gt;
                &lt;/td&gt;
              &lt;/tr&gt;
            &lt;/ItemTemplate&gt;
  
            &lt;LayoutTemplate&gt;
              &lt;table runat="server"&gt;
                &lt;tr runat="server"&gt;
                  &lt;td runat="server"&gt;
                    &lt;table id="itemPlaceholderContainer" runat="server" border="0" style="" class="table table-striped"&gt;
                      &lt;tr runat="server" style=""&gt;
                        &lt;th runat="server"&gt;&lt;/th&gt;
                        &lt;th runat="server"&gt;Indhold&lt;/th&gt;
                        &lt;th runat="server"&gt;BrugerNavn&lt;/th&gt;
                        &lt;th runat="server"&gt;Postnummer&lt;/th&gt;
                        &lt;th runat="server"&gt;Emne&lt;/th&gt;
                      &lt;/tr&gt;
                      &lt;tr id="itemPlaceholder" runat="server"&gt;
                      &lt;/tr&gt;
                    &lt;/table&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;td&gt;
                    &lt;asp:Button ID="SkrivOpslag" runat="server" CommandName="SkrivOpslag" Text="Skriv Opslag" CssClass="btn btn-default btn-xs" OnClick="SkrivOpslag_Click" /&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr runat="server"&gt;
                  &lt;td runat="server" style=""&gt;
                    &lt;asp:DataPager ID="DataPager1" runat="server"&gt;
                      &lt;Fields&gt;
                        &lt;asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" FirstPageText="Første Side" ShowLastPageButton="True" LastPageText="Sidste Side" PreviousPageText="Forrige" NextPageText="Næste" ButtonCssClass="btn btn-default" /&gt;
                      &lt;/Fields&gt;
                    &lt;/asp:DataPager&gt;
                  &lt;/td&gt;
                &lt;/tr&gt;
              &lt;/table&gt;
            &lt;/LayoutTemplate&gt;
  
            &lt;SelectedItemTemplate&gt;
              &lt;tr style=""&gt;
                &lt;td&gt;
                  &lt;asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" CssClass="btn btn-default btn-xs" /&gt;
                  &lt;asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" CssClass="btn btn-default btn-xs" /&gt;
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:Label ID="IndholdLabel" runat="server" Text='&lt;%# Eval("Indhold") %&gt;' /&gt;
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:Label ID="BrugerNavnLabel" runat="server" Text='&lt;%# Eval("BrugerNavn") %&gt;' /&gt;
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:Label ID="PostnummerLabel" runat="server" Text='&lt;%# Eval("Postnummer") %&gt;' /&gt;
                &lt;/td&gt;
                &lt;td&gt;
                  &lt;asp:Label ID="EmneLabel" runat="server" Text='&lt;%# Eval("Emne") %&gt;' /&gt;
                &lt;/td&gt;
              &lt;/tr&gt;
            &lt;/SelectedItemTemplate&gt;
          &lt;/asp:ListView&gt;
        &lt;/center&gt;
  
        &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" EnableViewState="True" ConnectionString="&lt;%$ ConnectionStrings:foradbConnectionString %&gt;" DeleteCommand="DELETE FROM [testOpslagstabel] WHERE [OpslagsID] = @OpslagsID" InsertCommand="INSERT INTO [testOpslagstabel] ([Indhold], [DatoTid], [Reference], [BrugerNavn], [Emne], [Postnummer]) VALUES (@Indhold, GetDate(), @Reference, 'testuser', @Emne, @Postnummer)"
        SelectCommand="SELECT * FROM [testOpslagstabel] WHERE ([Postnummer] = @Postnummer)" UpdateCommand="UPDATE [testOpslagstabel] SET [Indhold] = @Indhold, [DatoTid] = @DatoTid, [Reference] = @Reference, [BrugerNavn] = 'testuser', [Postnummer] = @Postnummer, [Emne] = @Emne WHERE [OpslagsID] = @OpslagsID"
        InsertCommandType="Text"&gt;
          &lt;DeleteParameters&gt;
            &lt;asp:Parameter Name="OpslagsID" Type="Int32" /&gt;
          &lt;/DeleteParameters&gt;
          &lt;InsertParameters&gt;
            &lt;asp:Parameter Name="Indhold" Type="String" /&gt;
            &lt;asp:Parameter Name="DatoTid" Type="DateTime" /&gt;
            &lt;asp:Parameter Name="Reference" Type="Int32" /&gt;
            &lt;asp:Parameter Name="BrugerNavn" Type="String" /&gt;
            &lt;asp:QueryStringParameter Name="Postnummer" QueryStringField="Postnummer" Type="Int32" /&gt;
            &lt;asp:Parameter Name="Emne" Type="String" /&gt;
          &lt;/InsertParameters&gt;
          &lt;SelectParameters&gt;
            &lt;asp:QueryStringParameter Name="Postnummer" QueryStringField="Postnummer" Type="Int32" /&gt;
          &lt;/SelectParameters&gt;
          &lt;UpdateParameters&gt;
            &lt;asp:Parameter Name="Indhold" Type="String" /&gt;
            &lt;asp:Parameter Name="DatoTid" Type="DateTime" /&gt;
            &lt;asp:Parameter Name="Reference" Type="Int32" /&gt;
            &lt;asp:Parameter Name="BrugerNavn" Type="String" /&gt;
            &lt;asp:QueryStringParameter Name="Postnummer" QueryStringField="Postnummer" Type="Int32" /&gt;
            &lt;asp:Parameter Name="Emne" Type="String" /&gt;
            &lt;asp:Parameter Name="OpslagsID" Type="Int32" /&gt;
          &lt;/UpdateParameters&gt;
        &lt;/asp:SqlDataSource&gt;
      &lt;/asp:Panel&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p><strong><em>Just to make everything more clear I'm updating the post with the full code in my code behind the aspx. Also if it makes the understanding better, I'm trying to create a forum.</em></strong></p>
  
  <pre><code>using System;
  using System.Configuration;
  using System.Data.SqlClient;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  namespace initial.site
  {        
      public partial class forum_front : System.Web.UI.Page
      {
          string qbynavn;
          object objbynavn;
  
          // Makes the SQL connection string
          String CS = ConfigurationManager.ConnectionStrings["FORADB"].ConnectionString;
  
          protected void Page_Load(object sender, EventArgs e)
          {    
              string qpostnr = Request.QueryString["Postnummer"];
              if (qpostnr != null)
              {
                  try
                  {
                      using (SqlConnection con = new SqlConnection(CS))
                      {
                          // specifies the command to check for zipcode
                          SqlCommand cmd = new SqlCommand("SELECT Bynavn FROM Postnummertabel WHERE Postnr = " + qpostnr, con);
                          // Opens the connection
                          con.Open();
  
                          objbynavn = cmd.ExecuteScalar();
                          qbynavn = objbynavn.ToString();
  
                          ForumOverskrift.Text = " Velkommen til " + qbynavn;
                      }
                  }
                  catch (Exception ex)
                  {
                      Response.Write("Der opstod en fejl! " + ex.Message);
                  }
              }
              else
              {
                  ForumOverskrift.Text = " Velkommen!";
              }
           }
  
          public void AnswerButton_Click(object sender, EventArgs e)
          {
              // Tries to bind the sender to the right button.
              Button originator = sender as Button;
  
              // Checks if it has been found
              if (originator != null)
              {
                  // Goes throug the control hierachy to find the right item.
                  var parentItem = originator.Parent as ListViewItem;
                  if (parentItem != null
                       &amp;&amp; parentItem.ItemType == ListViewItemType.DataItem)
                  {
                      // Binds the textbox and button to variables
                      var textBox = parentItem.FindControl("AnswerTextBox") as TextBox;
                      var btn = parentItem.FindControl("AnswerButton") as Button;
  
                      if (textBox != null)
                      {
                          // Changes the textbox to being visible and changes the buttons text.
                          if (textBox.Visible == false)
                          {
                              textBox.Visible = true;
                              btn.Text = "Fortryd";
                          }
                          // Changes the textbox to invisible and changes the buttons text.
                          else if (textBox.Visible == true)
                          {
                              textBox.Visible = false;
                              btn.Text = "Svar";
                          }
                      }
                  }
              }
          }
  
          // Makes the Skriv Opslag field either visible or invisible
          protected void SkrivOpslag_Click(object sender, EventArgs e)
          {
              if (ListView1.InsertItem.Visible == true)
              {
                  // Makes the Skriv Opslag field invisible
                  ListView1.InsertItem.Visible = false;
  
                  // Changes the buttons name to Skriv Opslag
                  Button btn = (Button) ListView1.FindControl("SkrivOpslag");
                  btn.Text = "Skriv Opslag";
              }
              else if (ListView1.InsertItem.Visible == false)
              {
                  // Makes the Skriv Opslag field visible
                  ListView1.InsertItem.Visible = true;
  
                  // Changes the Buttons name to Skriv Opslag
                  Button btn = (Button)ListView1.FindControl("SkrivOpslag");
                  btn.Text = "Fortryd";
              }
          }
  
          protected void TilForsiden_Click(object serder, EventArgs e)
          {
              Response.Redirect("~/welcomepage.aspx");
          }
  
          protected void ListView1_ItemDeleting(object sender, ListViewDeleteEventArgs e)
          {
             ClientScript.RegisterStartupScript(GetType(), "hwa", "deletealert();", true);
  
             //e.Cancel = true;
  
              //Response.Write("&lt;script&gt;deletealert();&lt;/script&gt;");
              //ScriptManager.RegisterClientScriptBlock(this, GetType(), "mykey", "deletealert();", true);
          }
      }
  }
  </code></pre>