[英]How to display confirm dialog from C# to client and use the result
我有一个通用处理程序,该处理程序在得到用户确认后才从某个位置删除文件,这是他们真正想要的。
我的代码是:
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确认对话框实现同一目的?
从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?");
}
有一些不错的UI可用于显示确认框。 查看jQuery Modal对话框或Bootstrap引导框等
您不能那样做,因为您的处理程序在服务器上执行。 相反,您将必须使用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" });
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.