2011-09-23 48 views
0

我正在使用JavaScript函數打印頁面。我不斷得到字符串作爲空值,我不知道如何.....這裏是代碼。我所擁有的div被稱爲divSheet,並將其設置爲可見的false以開始......當您加載信息時,它會在divSheet中創建一個表並將其設置爲true。任何想法爲什麼它說getPrint函數strid是空的?謝謝!用javascript打印問題..... div來作爲空字符串

  <asp:ImageButton runat="server" ID="imageBtnPrint" Style="z-index: 100" ImageUrl="~/Images/printerIcon.gif" 
           OnClientClick="javascript:getPrint('divSheet')" ToolTip="Print" /> 


    function getPrint(strid) 
    { 
     var pp = window.open(); 
     var prtContent = document.getElementById(strid); 
     pp.document.writeln('<HTML><HEAD><title>Print Confirmation Sheet</title><LINK href=PrintStyle.css type="text/css" rel="stylesheet">') 
     pp.document.writeln('<LINK href=PrintStyle.css type="text/css" rel="stylesheet" media="print"><base target="_self"></HEAD>') 
     pp.document.writeln('<body MS_POSITIONING="GridLayout" bottomMargin="0" leftMargin="0" topMargin="0" rightMargin="0">'); 
     pp.document.writeln('<form method="post">'); 
     pp.document.writeln('<TABLE width=100%><TR><TD></TD></TR><TR><TD align=right><INPUT ID="PRINT" type="button" value="Print" onclick="javascript:location.reload(true);window.print();"><INPUT ID="CLOSE" type="button" value="Close" onclick="window.close();"></TD></TR><TR><TD></TD></TR></TABLE>'); 
     pp.document.writeln(document.getElementById(strid).innerHTML); 
     pp.document.writeln('</form></body></HTML>'); 

    } 
+0

你能分享整個頁面代碼嗎,上面的'divSheet'元素缺失。 – Saket

+0

它有太多的角色讓我發帖....虐待把它的縮短版本在這裏。 – Lucas

+0

Lucas

回答

0

我想那是因爲你是從你的主HTML生成一個子窗口,子頁面無法直接訪問父的變量。

你可以把它用在父HTML到您的div的ID設置局部變量,然後從你的JS函數訪問:window.opener.<variable name>

事情是這樣的: (警告:未經測試)

... 
var divId; 
function getPrint(strid) 
{ 
    divId = strid; 
    var pp = window.open(); 
    var prtContent = document.getElementById(window.opener.divId); 
    ... 
    ... 
}