vb.net JavaScript 运行时错误:无法在 asp.net 中获取未定义或空引用的属性“innerHTML”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16512336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:34:19  来源:igfitidea点击:

JavaScript runtime error: Unable to get property 'innerHTML' of undefined or null reference in asp.net

javascriptasp.netvb.netprinting

提问by Vishal

I have a textbox in which user will write some text.

我有一个文本框,用户将在其中写入一些文本。

Now If he wants to print that text he should click on print button. The design of Print button is as follows:

现在,如果他想打印该文本,他应该单击打印按钮。打印按钮的设计如下:

<asp:Button ID="btnPrint" runat="server" Text="Print" Enabled="False" OnClientClick = "CallPrint('txtReadFiles')" />

Now in the head part of aspx page the function CallPrint is declared as follows :

现在在 aspx 页面的头部,函数 CallPrint 声明如下:

<script type="text/javascript" language="javascript">
        function CallPrint(strid)
        {
            var prtContent = document.getElementById(strid);
            **var docwrite=prtContent.innerHTML;**
            var WinPrint = window.open('','','left=0,top=0,toolbar=0,status=0');
            WinPrint.document.open();
            WinPrint.document.write(docwrite);
            WinPrint.document.close();
            WinPrint.focus();
            WinPrint.print();
            WinPrint.close();
        }
    </script>

But when I click on print button I get an error on line marked with **

但是当我点击打印按钮时,我在标有 ** 的线上出现错误

JavaScript runtime error: Unable to get property 'innerHTML' of undefined or null reference

JavaScript 运行时错误:无法获取未定义或空引用的属性“innerHTML”

What is the problem here?

这里有什么问题?

回答by varun

thats probably because this line var prtContent = document.getElementById(strid);fails i.e. returns null. is the textbox you are trying to get also a server control ? If so then during runtime its id would change ,if this indeed is the issue look here

那可能是因为该行var prtContent = document.getElementById(strid);失败,即返回空值。您尝试获取的文本框也是服务器控件吗?如果是这样,那么在运行时它的 id 会改变,如果这确实是问题,请看这里