我有一个通用处理程序,在从用户那里获得他们真正想要的确认后,从某个位置删除文件。
我的代码是:
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='<%# Container.DataItemIndex %>' 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确认对话框实现相同的功能?
答案 0 :(得分:1)
从javascript和流程服务器获取确认点击
<asp:ImageButton runat="server" OnClientClick="return getConfirmation()"
ToolTip="Delete File" ID="lnkDelete" OnClick="DeleteFile"
CommandArgument='<%# Container.DataItemIndex %>'
ImageUrl="~/delete.png" Width="50px" Height="50px" />
然后JS代码
function getConfirmation(){
return window.confirm("Do you really want to delete the file?");
}
有一些不错的用户界面可用于显示确认框。查看jQuery Modal对话框或bootstrap bootbox等
答案 1 :(得分:1)
您不能这样做,因为您的处理程序在服务器上执行。相反,您将不得不使用JavaScript来决定是否删除该文件。例如:
<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" });
}