<script  type="text/javascript">
     function CallServer(inputcontrol,context)
     {
        //回调还没有处理完全时其预先加载的显示值
         context.innerHTML = "加载中......";
        //为你在文本框中输入的信息,并且arg在这里就是将其值传递到
        //RaiseCallbackEvent(String eventArgument)方法对应的eventArgument中
         arg = inputcontrol.value;
         alert(arg);
        //获取一个对客户端函数的引用;调用该函数时,将启动一个对服务器端事件的客户端回调。
 
        <%=ClientScript.GetCallbackEventReference(Page, "arg", "ReceiveServerData","")%>; 
        function ReceiveServerData(result)
         {
              context.innerHTML=result;
         }  
}
</script>
 请输入信息:<asp:TextBox ID="txtEnter" runat="server"></asp:TextBox>
        <input id="btnSubmit" type="button" value="提交" onclick="CallServer(txtEnter,lblShow)"/>
        <asp:Label ID="lblShow" runat="server" Text="Label"></asp:Label></div>这段代码我原来想遵循加载dom的原则,最后加载脚本文件,但是老报缺少对象,事实是执行到
<%=ClientScript.GetCallbackEventReference(Page, "arg", "ReceiveServerData","")%>; 
就报错,缺少对象。但是后来我把它放在<head>标签内,就不报错啦,这是为什么?按理说脚本文件放在最后加载该初始化的对象,都已经初始化了,这是什么原因,有谁能解释一下原理。

解决方案 »

  1.   

    在客户端的onload里做你要做的
    window.onload=function(){
     //do something....
    }
      

  2.   

    缺少的是什么对象呢?
    是加载时候出错还是onclick出错呢
      

  3.   

    onclick时出错,onclick执行到
    <%=ClientScript.GetCallbackEventReference(Page, "arg", "ReceiveServerData","")%>; 
    页面报脚本错误!
      

  4.   

    window.onload=function(){
        //do something
    }这个是触发某个函数后才能做这个事件,不要开始就执行