我在页面定义了一个输入框:
<asp:TextBox ID="txtCountryID" Runat="server" Width="10" Visible=False>0</asp:TextBox>
注意,我设置一个Visible=False。
然后我在OnLoad的JS事件中,用了以下语句:
function loadme()
{
   alert(document.forms[0].txtCountryID.value);
}
这样,报错说“document.forms.0.txtCountryID.value为空或不是对象”如果我把Visible=False去掉就可以了,为什么呢?
JS是不是换一种写法就可以了?谢谢

解决方案 »

  1.   

    控件不可见是不会被加入控件树的,也就是说它在document中是不存在的
      

  2.   

    设成visible在客户端看不到要实现相同效果可以用<input type=hidden runat=server id='txtCountryID'/>
      

  3.   

    <asp:TextBox ID="txtCountryID" Runat="server" Width="10" style="display:none">0</asp:TextBox>
      

  4.   

    孟子兄说的对,要用样式来把控件隐藏。显示的时候只需设置display=''即可。
      

  5.   

    style="display:none"  支持孟子!
    不显示,但document生成
      

  6.   

    if(document.forms[0].txtCountryID)
        alert(document.forms[0].txtCountryID.value);
    else
        alert('啊哈');