[問題] .NET 動態新增的LinkButton 與檔案下載

看板Web_Design作者 (亞妹露~!!)時間13年前 (2012/06/15 19:11), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串1/1
基本上在逐步執行的時候值都是對的 可是執行結束呈現的是這個畫面:http://ppt.cc/hWDG 然後多種文字檔、.MP3都是直接被瀏覽器開啟了。 以下是於 Page_Load 新增 LinkButton 於 Literal 中: 前略... DataTable DT = DBClass.SQLSelect(strSQL,select);//撈資料到DT if (DT.Rows.Count > 0) { for (int i = 0; i < DT.Rows.Count; i++) { if(i != 0) DivLiteral.Control.Add(new LiteralControl(" ");//排版用空格 else DivLiteral.Control.Add(new LiteralControl("附件下載"); LinkButton lbtn = new LinkButton(); lbtn.ID = "lbtn_fileDownload_" + i.ToString(); lbtn.Text = DT.Rows[i][0].ToString(); lbtn.CommandArgument = DT.Rows[i][1].ToString();//這裡放檔案名稱 lbtn.Click += new EventHandler(lbtn_download_click);//加上事件 DivLiteral.Control.Add(lbtn); DivLiteral.Control.Add(new LiteralControl("<br />")); } } 以上 沒有使用 UpdatePanel 僅使用到 Panel 和 Div 網頁開起來都對 以下是按鈕事件(檔案下載): protected void lbtn_download_click(object sender, EventArgs e) { string fileName = ((LinkButton)sender).CommandArgument.ToString(); string downloadPath = System.Configuration.ConfigurationManager.AppSettings ["DownloadPath"].ToString() + fileName;//從Web.Config取得檔案路徑+檔名 if(HttpContext.Current.Request.Browser.Browser == "IE") fileName = HttpContext.Current.Server.UrlEncode(fileName); Response.ContentType = "Application/octet-stream"; Response.AppendHeader("Context-Disposition", string.Format(@"attchment; filename=" + fileName)); Response.TransmitFile(DownloadPath); Response.End(); } 按鈕事件其實在其他頁面都是成功下載,但是在這動態新增的就變成這樣… 請問有高手可以提點一下嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.85.122.181 ※ 編輯: YaMeiLo 來自: 219.85.122.181 (06/15 19:32)

06/17 00:39, , 1F
@@有強者可以給個方向或關鍵字嗎?真的沒有頭緒~...
06/17 00:39, 1F

06/17 02:47, , 2F
可能是postback的原因 動態新增在每一次postback的時候
06/17 02:47, 2F
文章代碼(AID): #1FsnXYi- (Web_Design)