0

I have a generic handler which deletes a file from a location after getting a confirmation from the user that is what they really want.

My code is:

public class DeleteFilePDF : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        System.Web.HttpRequest request2 = System.Web.HttpContext.Current.Request;
        string strSessVar2 = request2.QueryString["fileVar"];

        //MessageBox.Show(strSessVar2);
        if (File.Exists(strSessVar2))
        {
            DialogResult dlgRes = MessageBox.Show("Do you really want to delete the file?", "Program Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dlgRes == DialogResult.Yes)
            {
                try
                {
                    File.Delete(strSessVar2);
                    HttpContext.Current.Response.Redirect("PDFAllFilesDisplay.aspx", false);
                }
                catch (Exception ce)
                {
                }
            }
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

My ImageButton code:

<asp:ImageButton runat="server" ToolTip="Delete File" ID="lnkDelete" OnClick="DeleteFile" CommandArgument='<%# Container.DataItemIndex %>' ImageUrl="~/delete.png" Width="50px" Height="50px" />

My ImageButton code-behind:

protected void DeleteFile(object sender, EventArgs e)
    {
        string strFile = GridView1.Rows[Convert.ToInt32(((ImageButton)sender).CommandArgument.ToString())].Cells[0].Text;
        string strFolderFile = strDirectory + strFile;
        //MessageBox.Show(strFolderFile);
        Response.Redirect("DeleteFilePDF.ashx?fileVar=" + strFolderFile);
    }

Everything works as it should in debugging environment but outside of that I am not able to use MessageBox.Show() function. How can I achieve the same thing using a JQuery/JavaScript confirm dialog?

Si8
  • 9,141
  • 22
  • 109
  • 221
  • In javascript you can use a confirm dialog. – Bayeni Jul 01 '14 at 14:06
  • I updated my question to explain more of what I am doing... How would I implement it – Si8 Jul 01 '14 at 14:08
  • 1
    Side note: since your your code is just sample it is ok to have serious security issues (the only one that generally will get you immediate downvotes is SQL injection so far). In future consider not posting code with obvious issues - check out [one-click attack](http://en.wikipedia.org/wiki/Cross-site_request_forgery). – Alexei Levenkov Jul 01 '14 at 14:18
  • I am using session so if the session variable sent doesn't match what the sending page had my code doesn't execute. – Si8 Jul 01 '14 at 14:40

2 Answers2

1

Get the confirmation from javascript and process server click

<asp:ImageButton runat="server" OnClientClick="return getConfirmation()" 
     ToolTip="Delete File" ID="lnkDelete" OnClick="DeleteFile" 
     CommandArgument='<%# Container.DataItemIndex %>' 
     ImageUrl="~/delete.png" Width="50px" Height="50px" />

Then JS code

  function getConfirmation(){
    return window.confirm("Do you really want to delete the file?");
  }

There are some nice UIs available for showing a confirmation box. Check out jQuery Modal dialog or bootstrap bootbox, etc

Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
  • I am using a GridView's CommandArguement to get the value. So I am not sure if it will work. – Si8 Jul 01 '14 at 14:10
  • I actually ended up using something similar to yours. Thanks for the response. – Si8 Jul 01 '14 at 14:14
1

You can't do it that way because your handler is executed on the server. Instead you will have to use JavaScript to decide whether or not to delete the file. for example:

<input type="button" onclick="deleteFile()" value="Delete File" title="Press to delete file" />

function deleteFile {
    //show dialog with jquery or anything similar
    //if yes is selected, then make the handler call using ajax. for example using jquery ajax: 
   $.ajax({ url: [handler url] + [query string], method: "post" });
}