简体   繁体   English

ASP.NET MasterPage + Javascript错误

[英]ASP.NET MasterPage + Javascript Error

After converting an ASP.NET webform (v3.5) to use a master page, I started getting a strange Javascript error. 将ASP.NET Web表单(v3.5)转换为使用母版页后,我开始收到一个奇怪的Javascript错误。

The content page has a block of javascript. 内容页面上有一块JavaScript。 Here it is: 这里是:

 <script type="text/javascript">

    var attCount = 0;

    function CreateAttachmentControl() {
        var newAtt = document.createElement('span');
        var newAttName = 'area' + attCount;
        newAtt.setAttribute('id', newAttName);
        newAtt.setAttribute('name', newAttName);

        var newInput = document.createElement('input');
        var newInputName = 'att' + attCount;
        newInput.setAttribute('type', 'file');
        newInput.setAttribute('id', newInputName);
        newInput.setAttribute('name', newInputName);

        if (newInput.addEventListener) {
            newInput.addEventListener("onchange", CreateAttachmentControl, false);
        } else if (newInput.attachEvent) {
            newInput.attachEvent("onchange", CreateAttachmentControl);
        } else {
            newInput.onchange = CreateAttachmentControl;
        }

        var newRemove = document.createElement('a');
        newRemove.setAttribute('href', 'javascript:RemoveAttachmentControl("' + attCount + '")');
        newRemove.setAttribute('title', 'Remove this attachment');
        newRemove.innerHTML = 'X';
        newAtt.appendChild(newInput);
        newAtt.appendChild(document.createTextNode(' '));
        newAtt.appendChild(newRemove);
        newAtt.appendChild(document.createElement('br'));
        attArea.appendChild(newAtt); // error here

        attCount++;
    }

    function RemoveAttachmentControl(n) {

        // get input element
        var input = document.getElementById('att' + n);

        // if the input is blank dont delete
        if (input.value != '' && input.value != null) {
            var att = document.getElementById('area' + n);
            attArea.removeChild(att);
        }
    }
</script>

The error is: 'attArea' is undefined 错误是:“ attArea”未定义

But, I know that it is not, because just below my block of javascript is this: 但是,我知道不是,因为在我的JavaScript块下面是这样的:

...<td align="left" colspan="2" style="height: 13px" id="attArea" name="attArea"></td>...

This working perfectly before I converted the webform into content page with a master page. 在将Webform转换为带有母版页的内容页之前,这种方法非常有效。 Is there some known Javascript + Masterpage issues? 是否存在一些已知的Javascript + Masterpage问题?

Thanks 谢谢

In the code sample you provided, attArea is undefined. 在您提供的代码示例中,attArea 未定义的。 The first reference to it you are calling attArea.appendChild() . 对其的第一个引用是调用attArea.appendChild() Is it declared somewhere higher in the source that you didn't provide? 是否在您未提供的来源中声明了更高的位置?

I think you are missing: 我认为您不见了:

var attArea = document.getElementById('attArea');
attArea.appendChild(newAtt); // no more error!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM