HTML文件:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TransferReg.aspx.cs" Inherits="W90.Web.Member.TransferReg" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
   
    <script src="js/PCA.js" type="text/javascript"></script>
</head>
<body>
<TABLE>
 <tr>
            <td valign="top">
              地址:</td>
            <td>
<!--省市区-->
省:<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
市:<asp:DropDownList id="DropDownList2" runat="server"></asp:DropDownList>
区:<asp:DropDownList id="DropDownList3" runat="server"></asp:DropDownList>&nbsp;
            </td>
        </tr>
</TABLE><body>
js文件:
/*
xml+ajax无刷新联动省市区功能
create by tongwh
2008.12.18
*/
function CityResult(parent_id,objDropDownList){
    document.getElementById("<%=this.lbl_provice.ClientID%>").value = parent_id.text;
    //alert(parent_id.text);
    //alert(document.getElementById("lbl_provice").value);
    document.getElementById("<%=this.lbl_provice_id.ClientID%>").value = parent_id.value;
    WebForm1.BindCity(parent_id.value,objDropDownList,get_City_Result_CallBack);
}
function get_City_Result_CallBack(response)
{
    if (response.value != null)
    {                               
    var ds = response.value;
    var first_city;
    var first_city_id;
    //绑定城市
        if(ds != null && typeof(ds) == "object" && ds.Tables != null){               
document.getElementById("<%=this.DDLCity.ClientID%>").length = 0;
            for(var i=0; i<ds.Tables[0].Rows.length; i++){
            var name=ds.Tables[0].Rows[i].Text;
          var id=ds.Tables[0].Rows[i].Value;
          if(i==0){first_city = name;first_city_id = id};
          document.getElementById("<%=this.DDLCity.ClientID%>").options.add(new Option(name,id));
        }
        //每次省选择以后,赋省下第一个城市值
        document.getElementById("<%=this.lbl_city.ClientID%>").innerText = first_city;
//绑定地区
if(first_city_id&&typeof(first_city_id)!='undefined')
    WebForm1.BindArea(first_city_id, '<%=this.DDLArea.ClientID%>"', get_Area_Result_CallBack);
     else{
     document.getElementById("<%=this.DDLArea.ClientID%>").length = 0;
     }
        }
       
    }           
}function AreaResult(parent_id,objDropDownList){
    document.getElementById("<%=this.lbl_city.ClientID%>").innerText = parent_id.text;
    WebForm1.BindArea(parent_id.value,objDropDownList,get_Area_Result_CallBack);
}
function get_Area_Result_CallBack(response)
{
    if (response.value != null){                   
    var ds = response.value;
    var first_area;
    var first_area_id;
        if(ds != null && typeof(ds) == "object" && ds.Tables != null){                   
document.getElementById("<%=this.DDLArea.ClientID%>").length = 0;
            for(var i=0; i<ds.Tables[0].Rows.length; i++){
            var name=ds.Tables[0].Rows[i].Text;
          var id=ds.Tables[0].Rows[i].Value;
          if(i==0){first_area = name;first_area_id = id};
          document.all("<%=this.DDLArea.ClientID%>").options.add(new Option(name,id));
        }
        //每次城市后 赋第一个地区值
         document.getElementById("<%=this.lbl_area.ClientID%>").innerText = first_area;
        }
    }
}
function AreaResultV(parent_id,objDropDownList){
    document.getElementById("<%=this.lbl_area.ClientID%>").innerText = parent_id.text;
    //dropdownlist_ajaxpro_Default.BindArea(parent_id.value,objDropDownList,get_Area_Result_CallBack);
}
  问题:  
如果把以上的JS代码直接复制到ASPX页面里则不会出错,功能可以正常。但是如果把以上的JS保存到JS文件,然后调用则会出现      :[color=#000000]调用外部JS文件后出现JavaScript中getElementById()为空或不是对象
   的问题。请MSDN上的朋友指点指点。谢谢!  [/color]

解决方案 »

  1.   

    必要直接写在页面上。<%=this.lbl_area.ClientID%> 它被认为是id="<%=this.lbl_area.ClientID%>",而页面根本没有。
      

  2.   

    对,调用js方法的这句代码要放到html标签出现之后,否则元素还没生成,自然会出错。
      

  3.   

    只有在aspx页面里面的<%=this.lbl_city.ClientID%>这些才会被替换成他们各自的ClientID。
    显然你写到js文件里面,是不行的。
    不知道我这么说你明白没有?
      

  4.   

    外部js文件中是不可以使用<%=this.DDLCity.ClientID%>这个的,你可以看看http://blog.csdn.net/zhou_xuexi/article/details/6620312这个,有间接方法可以实现