Parent.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Parent.aspx.cs" Inherits="Parent" %><!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></head>
<body>
    <form id="pForm" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Text="11111111111"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> 
    </div>
    </form>
</body>
    <script type="text/javascript">
        function show()
        {
            alert(document.getElementById('TextBox1').value);
        }
    </script>
</html>Parent.aspx.cs
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "test", "show();", true);
    }错误:
alert(document.getElementById('TextBox1').value);
缺少对象怎么解决

解决方案 »

  1.   

    客户端生成的ID和服务器端控件的ID肯定是不一样的.
      

  2.   

    document.getElementById('TextBox1').value
    在客户端页面里,没有ID为"TextBox1"的对像。TextBox1是在服务器端控件的ID。在页面中会生成其它ID
      

  3.   

    ID一样的。。 因为没加masterpage
      

  4.   

    改成下面的:alert(document.getElementById("<%='TextBox1.ClientID %>").value); 
      

  5.   

    因为页面重新加载 在页面加载完之前 就调用了document.getElementById('TextBox1').value 
      

  6.   

    无奈了,你试下。alert(document.getElementById("<%='TextBox1.ClientID %>").value); 去掉protected void Button1_Click(object sender, EventArgs e)事件后在button中添加OnClientClick事件OnClientClick=”show()“
      

  7.   

    ....
    楼上的。。 我就要这样的效果
    我知道OnClientClick 能取到。。
      

  8.   

    我要在protected void Button1_Click(object sender, EventArgs e)事件 
    取到!
      

  9.   

    我试验成功,如下:前台
    <script type="text/javascript"> 
    function show() 

         alert(document.getElementById("<%=TextBox1.ClientID %>").value); 

    </script> 后台文件
    protected void Button1_Click(object sender, EventArgs e)
    {
            
         RegisterStartupScript("show", "<script>show();</script>");}
      

  10.   


    ClientScript.RegisterClientScriptBlock  不能处理弹出的窗口,还有其他的一些限制
      

  11.   

    RegisterStartupScript和RegisterClientScriptBlock不同,RegisterStartupScript返回的函数在 document装载完成后会执行。
      

  12.   

    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "test", "show();", true); 
    =>
    this.ClientScript.RegisterStartupScript(this.GetType(), "test", "show();", true);