【发布时间】:2017-05-12 10:36:48
【问题描述】:
我试图在图像按钮 onClientClick 事件上将 PDF 文件打开到新窗口,每当我第一次单击图像按钮时,什么都不会发生,当我尝试第二次单击时,它将在新窗口中打开文件 我只想在第一次点击时打开文件
我使用了下面提到的代码。
//Find control in Grid view
GridViewRow row = (GridViewRow (((ImageButton)e.CommandSource).NamingContainer);
HiddenField hdnFileName = (HiddenField)row.FindControl("hdnFilePath");
ImageButton ibtn = (ImageButton)row.FindControl("ibtnDownload");
//Get File Path and FileName and Concate
string strFilePath = "\\OnlineContent\\";
string filePath = strFilePath +"/"+ hdnFileName.Value;
//Open PDF file into new tab
ibtn.OnClientClick = "window.open('"+filePath.ToString()+"','target=_blank');";
【问题讨论】:
-
你知道你的字符串变量 filePath 被初始化为:"\\OnlineContent\\/{value}" 吗?
-
使用
window.open总是一个坏主意。许多浏览器/插件/病毒扫描程序都可以阻止它。 -
是的,我知道@MasterYoda
-
@WeeZy 但这又是如何工作的呢?它要么解析为文件路径,要么解析为 URL,不是吗? \\ 和 / 怎么一起使用?
-
好的 @VDWWD 任何其他可以帮助我在浏览器中打开 PDF 文件而不是下载的选项或链接,只需在新选项卡中打开该文件
标签: c# asp.net imagebutton