首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从C#向客户端显示确认对话框并使用结果

如何从C#向客户端显示确认对话框并使用结果
EN

Stack Overflow用户
提问于 2014-07-01 14:00:17
回答 2查看 1.8K关注 0票数 0

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

我的代码是:

代码语言:javascript
运行
复制
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代码:

代码语言:javascript
运行
复制
<asp:ImageButton runat="server" ToolTip="Delete File" ID="lnkDelete" OnClick="DeleteFile" CommandArgument='<%# Container.DataItemIndex %>' ImageUrl="~/delete.png" Width="50px" Height="50px" />

我的ImageButton代码-隐藏:

代码语言:javascript
运行
复制
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确认对话框实现相同的功能?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-01 14:09:39

从javascript和process单击

代码语言: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代码

代码语言:javascript
运行
复制
  function getConfirmation(){
    return window.confirm("Do you really want to delete the file?");
  }

有一些很好的UI可以显示一个确认框。查看jQuery模式对话框或引导引导程序等。

票数 1
EN

Stack Overflow用户

发布于 2014-07-01 14:14:52

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

代码语言: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" });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24512187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档