如何在C#中显示确认对话框并使用结果

我有一个通用的处理程序,在从用户那里得到他们真正想要的确认后,从一个位置删除一个文件。

我的代码是:

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; } } } 

我的ImageButton代码:

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

我的ImageButton代码隐藏:

 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); } 

一切都在调试环境中工作,但除此之外,我无法使用MessageBox.Show()函数。 如何使用JQuery / JavaScript确认对话框实现相同的function?

从javascript和进程服务器单击获取确认

  

然后是JS代码

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

有一些很好的用户界面可用于显示确认框。 查看jQuery Modal对话框或bootstrap bootbox等

你不能那样做,因为你的处理程序是在服务器上执行的。 相反,您将不得不使用JavaScript来决定是否删除该文件。 例如:

  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" }); }