runat =“server”+ javascript

时间:2011-10-16 06:17:17

标签: c# javascript asp.net vb.net

我的选择框有一个Javascript函数,但在我将runat =“server”包含在内之后,脚本调试器在下面的这一行中突出显示:

'for (i = 0; i < sourceTo.options.length; i++) {'并说:

SCRIPT5007:无法获取属性“length”的值:object为null或undefined

我包含它是因为我想在代码后面循环并执行其他一些东西,请提出建议谢谢!

代码:

    <select multiple size="8" style="width: 135px" runat="server" id="outletFromBox">
                                               <option value="JP">Jurong Point</option>
                                               <option value="IMM">IMM</option>
                                               <option value="Wisma">Wisma</option>                
                                           </select> 
                                       </td>

                                       <td align="center" valign="middle">
                                           <input type="button" class="ui-icon ui-icon-arrowthick-1-e ui-corner-all" style="width: 20px; height: 23px;" onClick=" javascript:move(('outletFromBox'),('outletToBox')); " 
                                                  value=""><br />
                                           <input type="button" class="ui-icon ui-icon-arrowthick-1-w ui-corner-all" style="width: 20px; height: 23px;" onClick=" javascript:move(('outletToBox'),('outletFromBox')); " 
                                                  value=""> 
                                       </td>
   <td style="padding-left: 4px"> 

                                   <select multiple size="8" style="width: 135px" runat="server" onBlur="selectAll(this, true, document.getElementById('<%=uilblDestinationQualOutlet.ClientID%>'))" id="outletToBox">
                                   </select> <span class="guide"></span>
                               </td></tr></table> 


                </div> 
            </asp:Panel>

使用Javascript:

function move(sourceFrom, sourceTo) {
            var arrFrom = new Array();
            var arrTo = new Array();
            var arrLU = new Array();
            var i;
            for (i = 0; i < sourceTo.options.length; i++) {
                arrLU[sourceTo.options[i].text] = sourceTo.options[i].value;
                arrTo[i] = sourceTo.options[i].text;
            }
            var fLength = 0;
            var tLength = arrTo.length;
            for (i = 0; i < sourceFrom.options.length; i++) {
                arrLU[sourceFrom.options[i].text] = sourceFrom.options[i].value;
                if (sourceFrom.options[i].selected && sourceFrom.options[i].value != "") {
                    arrTo[tLength] = sourceFrom.options[i].text;
                    tLength++;
                } else {
                    arrFrom[fLength] = sourceFrom.options[i].text;
                    fLength++;
                }
            }

            sourceFrom.length = 0;
            sourceTo.length = 0;

            var ii;
            for (ii = 0; ii < arrFrom.length; ii++) {
                var no = new Option();
                no.value = arrLU[arrFrom[ii]];
                no.text = arrFrom[ii];
                sourceFrom[ii] = no;
            }

            for (ii = 0; ii < arrTo.length; ii++) {
                var no = new Option();
                no.value = arrLU[arrTo[ii]];
                no.text = arrTo[ii];
                sourceTo[ii] = no;
            }

            (sourceTo).focus();

            if (sourceTo == (document.getElementById('outletFromBox'))) {
                (sourceFrom).focus();
            }
            if (sourceTo == (document.getElementById('QualMemTypeFromBox'))) {
                (sourceFrom).focus();
            }
            if (sourceTo == (document.getElementById('MemStatusFromBox'))) {
                (sourceFrom).focus();
            }
        }

1 个答案:

答案 0 :(得分:1)

当您向任何控件添加runat =“server”时,控件的客户端ID会更改。 我想这是你的问题,你应该将客户端id传递给移动函数。