有时会抛出Null Reference Exception

时间:2011-11-02 05:59:12

标签: javascript asp.net

<script language="javascript">
    res = "&res="+screen.width+"x"+screen.height+"&d="+screen.colorDepth
        top.location.href="Login.aspx?action=set"+res
    </script>
Source Error:
An unhandled exception was generated during the execution of the current web request,Information regarding origin and location exception can be identified using the exception stact trace below.

Stack trace:

[Null ReferenceException: object reference not set to an instance of an object.]
page_Load(object sender EventArgs e)+143
and next from System.Web

基于屏幕分辨率iam设置登录网页。如何避免空引用异常?

1 个答案:

答案 0 :(得分:1)

您应该考虑使用自动调整的基于流的页面布局。但是,我过去不得不做类似的事情,诀窍是将调整大小代码放在超时中:

setTimeout(function() {
    res = "&res="+screen.width+"x"+screen.height+"&d="+screen.colorDepth
    top.location.href="Login.aspx?action=set"+res
}, 0);

这是必需的,因为脚本代码不会确定性地执行。有时它会在计算和填充屏幕尺寸之前运行。其他Ajax工具包(如jQuery)通过将页面初始化代码放在特殊的onLoad处理程序中来处理这个确切的问题。