为什么我的代码打开两个窗口?

问题描述:

我试图在单独的选项卡/窗口中打开.pdf文件。它的工作,但它打开两个窗口来显示.pdf。我使用的代码如下。为什么我的代码打开两个窗口?

LinkButton btn = (LinkButton)(sender); 
string value = btn.CommandArgument; 
imfImageFile = LocalStaticData.UniImageResult; 
string path = imfImageFile.WindowsPath; 

if (path != "") 
{ 
    Session["OpenPDFImage"] = path;     
    ScriptManager.RegisterStartupScript(Parent.Page, GetType(), 
    Guid.NewGuid().ToString(), "openPdf(\"../InvoiceReport.aspx\");", true); 
} 

的JavaScript:

function openPdf(href) { 
    window.open(href); 
} 
+1

您是否在页面中放置了断点?它会突破两次吗?这可能是一个双重回寄麻烦? –

好了,所以两个问题 - 我想埃马努埃莱·格列柯是正确的,它是在你的页面周期被称为两次。第二个问题是你每次都给它一个唯一的代码。你应该放入相同的代码(不是Guid.NewGuid())来确保脚本只添加一次。

E.g.

LinkButton btn = (LinkButton)(sender); 
string value = btn.CommandArgument; 
imfImageFile = LocalStaticData.UniImageResult; 
string path = imfImageFile.WindowsPath; 
if (path != "") 
    { 
    Session["OpenPDFImage"] = path;     
    ScriptManager.RegisterStartupScript(Parent.Page, GetType(), 
    "InvoiceReportPDFOpenScript", "openPdf(\"../InvoiceReport.aspx\");", true); 
} 
+0

谢谢Emanuele Greco和rangitatanz。 Rangitatanz代码为我工作。再次感谢。如果你知道请帮忙,我还有一个问题。即时通讯在我的报告列表中使用自定义分页,分页不适合我,我可以为您复制代码。 – SunVigna

+0

最好是创建另一个问题 - 如果你喜欢,可以在这里链接到它。 –