2014-04-15 78 views
0

在ASP.Net MVC應用程序,我對視圖按鈕,並在單擊事件返回到這是文件下載後運行腳本

public void ExportToExcel() 
    { 
     string startDate = Convert.ToDateTime(Session["DateStart"]).ToString("yyyy-MM-dd"); 
     string endDate = Convert.ToDateTime(Session["DateEnd"]).ToString("yyyy-MM-dd"); 
     int hierarchyID = Convert.ToInt32(Session["HierarchyID"]); 

     DataBaseManager dbManager = new DataBaseManager(); 
     SqlConnection connection = new SqlConnection(dbManager.GetConnectionString()); 
     SqlCommand command = new SqlCommand("RPT_GetEmpTimeBoardForPeriodAndHierarchyPosition", connection); 
     command.CommandType = CommandType.StoredProcedure; 
     command.Parameters.Add(new SqlParameter("StartDate", startDate)); 
     command.Parameters.Add(new SqlParameter("EndDate", endDate)); 
     command.Parameters.Add(new SqlParameter("HierarchyID", hierarchyID)); 
     command.CommandTimeout = 360; 
     connection.Open(); 

     DataTable dt = new DataTable(); 
     DataSet ds = new DataSet(); 
     SqlDataAdapter da = new SqlDataAdapter(command); 
     da.Fill(dt); 

     GridView gv = new GridView(); 
     gv.DataSource = dt; 
     gv.DataBind(); 

     StringWriter sw = new StringWriter(); 
     HtmlTextWriter htw = new HtmlTextWriter(sw); 
     gv.RenderControl(htw); 

     Response.ClearContent(); 
     Response.Buffer = true; 
     Response.AddHeader("content-disposition", string.Format("attachment; filename=TabelFor_{0}-{1}.xls", 
          Convert.ToDateTime(Session["DateStart"]).ToString("dd.MM.yyyy"), 
          Convert.ToDateTime(Session["DateEnd"]).ToString("dd.MM.yyyy"))); 

     Response.ContentType = "application/ms-excel"; 
     Response.Charset = ""; 
     ViewBag.AlertMessage = "Download completed"; 

     Response.Output.Write(sw.ToString());   

     Response.Flush(); 
     Response.End(); 
    } 

的被點擊的時候我可以禁用按鈕的動作,但如何我可以在文件已被下載時啓用它。 提前致謝。

+0

我認爲這是你正在尋找的。 http://stackoverflow.com/questions/17364096/asp-net-mvc-return-file-and-redirect –

+0

我認爲這不是我想要的 – Iskuhi

+0

@Iskuhi你的意思是一旦下載完成,你想警惕? –

回答

0

我相信這應該觸發窗口加載事件,所以你可以綁定你的處理程序,以使按鈕將它,就像是

$(document).load(function(){ 
    $('#buttonId').prop("disabled", false); 
}); 

請注意,我沒有有機會與您的場景來測試它並且可能需要您在實際工作之前進行一些調整。

希望它有幫助!