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);
缺少对象怎么解决
<%@ 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);
缺少对象怎么解决
在客户端页面里,没有ID为"TextBox1"的对像。TextBox1是在服务器端控件的ID。在页面中会生成其它ID
楼上的。。 我就要这样的效果
我知道OnClientClick 能取到。。
取到!
<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>");}
ClientScript.RegisterClientScriptBlock 不能处理弹出的窗口,还有其他的一些限制
=>
this.ClientScript.RegisterStartupScript(this.GetType(), "test", "show();", true);