关于ASP.NET 页面生命周期个人感悟

 

系统中有一个页面,需要将值赋给一个前台JS的公共变量,在前台写报错:

<!-- 公用参数 -->
<script language="javascript" type="text/javascript">
    var jsLangFlag,jsLocationID,jsLocationRoom,jsTranCode;
    jsLangFlag=<%=session("LangFlag") %>;
    jsLocationRoom=0;

 jsLocationID = document.getElementById(MasterPageID + 'Location_drop').value;   //在此报错,因为控件还未加载完毕。
    jsTranCode=0;   //1为订购,入库
       
</script>

 

于是想到把此JS放到PAGE_LOAD去,但同样报错,找不到对象

   Protected  Sub page_load ( )
        Response.Write("<script> jsLocationID = document.getElementById(MasterPageID + 'Location_drop').value; </script>")
    End Sub

发现原因是一致的,因为当PAGE_LOAD的时候,控件其实还没有加载完毕,那  jsLocationID = document.getElementById(MasterPageID + 'Location_drop') 根本就找不到控件 。

后来便把此句放到RENDER去,成功了。

   Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        MyBase.Render(writer)
        Response.Write("<script> jsLocationID = document.getElementById(MasterPageID + 'Location_drop').value; </script>")
    End Sub

 

 

 

posted @ 2012-09-03 14:25  withoutaword  阅读(143)  评论(0编辑  收藏  举报