2011-11-26 109 views
0

我已经知道,Response.End()之后的代码不起作用。我在Response.End之后尝试alert a message box。基本上我在做什么是我会从Excel文件读取数据,并检查在Excel文件中缺少的数据。我将收集所有从Excel文件丢失的数据,并将它们全部写入text文件,并会提示使用下面的代码如何在Response.End后显示警报

string strPath = Server.MapPath("ErrorLog") + "\\" + "ErrorLog.txt"; 
string FileName = "Errorlog" + ".txt"; 
string[] createText = { sb.ToString() }; 
File.WriteAllLines(strPath, createText); 
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ""); 
strPath.Length.ToString()); 
HttpContext.Current.Response.ContentType = "application/text"; 
HttpContext.Current.Response.WriteFile(strPath); 
HttpContext.Current.Response.End(); 

在此之后我想显示一个警告框,说像一些事情Invalid Data。但我想知道我怎么能做到这一点Response.End

//更新的代码为每lcarus

string strPath = Server.MapPath("ErrorLog") + "\\" + "ErrorLog.txt"; 
string[] createText = { sb.ToString() }; 
File.WriteAllLines(strPath, createText); 
ClientScriptManager CSM = Page.ClientScript; 
string strconfirm = "<script>if(window.confirm('Excel file has Invalid data.')){window.location.href='/Excel1/ErrorLog/ErrorLog.txt'}</script>"; 
CSM.RegisterClientScriptBlock(this.GetType(), "Confirm", strconfirm, false); 

回答

1

你需要做的这几分2个部分:

收集丢失的数据,将其保存到一个文件中(顺便说一句,你需要为日志文件创建一个唯一的名字,除非你计划支持您的网站只有一个并发用户),并使用ClientScript.RegisterStartupScript调用一个函数是这样的:

ClientScript.RegisterStartupScript(this.GetType(),"somekey", 
"alert('Some data missing!'); window.location.href='http://your_site.com/path_to_log_file'",true); 

你会知道path_to_log_file,因为你是一个在服务器端生成它。

用户将看到警报,点击“确定”后,日志文件将显示在浏览器中或被要求下载,具体取决于浏览器是否知道如何处理文件(这通常由扩展名的文件)。

UPDATE

样品HttpHandler的实现。

您可以从后面的代码构建这样的链接:

window.location.href='FileDownloader.ashx?FileName=ErrorLog.txt' 

public class FileDownloader: IHttpHandler 
{ 
     public bool IsReusable 
     { 
      get { return true; } 
     } 

     public void ProcessRequest(HttpContext context) 
     { 
      // This is where you read the log file and add the content disposition 
      //header 
      string strPath = Server.MapPath("ErrorLog") + "\\" + context.Request.QueryString["FileName"]; 

      context.Response.AddHeader("Content-Disposition", "attachment; filename=" + context.Request.QueryString["FileName"] + ""); 
      strPath.Length.ToString()); 
      context.Response.ContentType = "application/text"; 
      context.Response.WriteFile(strPath); 
      context.Response.End(); 

     } 
} 
+0

因此,而不是'Httpcontext'我想使用'ClientScript'右 – Dotnet

+0

@用户:正确。您应该能够通过访问页面上的ClientScript属性来获取对ClientScriptManager对象的引用。 http://msdn.microsoft.com/en-us/library/system.web.ui.page.clientscript。aspx – Icarus

+0

但是我没有得到下载选项,我将用我的代码更新我的问题,你可以检查一次 – Dotnet

0

后先在你的页面类中声明受保护的字符串VAR:

public partial class YourASPPage : System.Web.UI.Page 
{ 
     protected string Alert = ""; 
     protected void Page_Load(object sender, EventArgs e) 
     { 
       Alert = "<script type='text/javascript'>alert('something alerted here');</script>"; 
     } 
} 

然后在您的aspx文件中,当您想要显示警报时(只是不在脚本标签中),写下类似的东西:

<%= Alert %> 
+0

我没有得到这个''<%=Alert%>好 – Dotnet

+0

我用iframe和按你说的写,但我仍不能达到要求的 – Dotnet

+0

<%= Alert %>把你在你的HTML代码声明保护串警报变种。因为它是一个脚本包含一个警报,只显示一个警报:),这是简单的方法:) –

-1

代码调用javascript函数到Response.End后面之后将无法正常工作();但是可以使用beforeunload,它会在Response.End()后触发;

$(window).bind('beforeunload', function(){ 
      //here your function 
    }); 
+0

这不适合我。 –