系统中有一个页面,需要将值赋给一个前台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