[英]How can i display an alert after Response.End
我已經知道, Response.End()
之后的代碼將無法工作。 我試圖在Response.End
之后alert
message box
。 基本上我在做什么是我將從Excel
文件中讀取數據並檢查excel文件中是否缺少數據。 我將收集Excel
文件中缺少的所有數據,並將它們全部寫入text
文件,並使用以下代碼提示您
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);
您需要分兩個部分來執行此操作:
收集丟失的數據,將其保存到文件中(順便說一句,除非您打算僅在站點上支持一個並發用戶,否則您需要為日志文件創建一個唯一的名稱),並使用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
因為您是在服務器端生成該文件的人。
用戶將看到警報,單擊“確定”后,日志文件將顯示在其瀏覽器上或要求下載該日志文件,具體取決於瀏覽器是否知道如何處理該文件(通常由文件擴展名決定) 。
更新
示例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();
}
}
首先在頁面類中聲明一個受保護的字符串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文件中,當您想顯示警報時(例如不在script標簽中)編寫類似的代碼:
<%= Alert %>
在Response.End()之后,無法從后面的代碼調用javascript函數。 但是可以使用beforeunload,它將在Response.End();之后觸發。
$(window).bind('beforeunload', function (){
//here your function
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.