2011-11-26 192 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

這不適合我。 –