Javascript ASP.NET document.getElementById('<%=Control.ClientID%>'); 返回空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8844675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET document.getElementById('<%=Control.ClientID%>'); returns null
提问by user346443
I'm trying to retrieve a server control in JavaScript. For testing purposes I am calling the JavaScript function from the page load event.
我正在尝试用 JavaScript 检索服务器控件。出于测试目的,我从页面加载事件调用 JavaScript 函数。
protected void Page_Load(object sender, EventArgs e){
ClientScript.RegisterClientScriptBlock(GetType(), "js", "confirmCallBack();", true);
}
And my JavaScript function is
我的 JavaScript 函数是
function confirmCallBack() {
var a = document.getElementById('<%= Page.Master.FindControl("PlaceHolderContent").FindControl("Button1").ClientID %>');
var b = document.getElementById('<%=Button1.ClientID%>');
}
my problem is that both a and b return null. Even when I view the page source the correct ClientID is returned.
我的问题是 a 和 b 都返回 null。即使当我查看页面源时,也会返回正确的 ClientID。
I should add that I'm using master page.
我应该补充一点,我正在使用母版页。
Any ideas.
有任何想法吗。
回答by Hector Sanchez
Gotcha!
知道了!
You have to use RegisterStartupScriptinstead of RegisterClientScriptBlock
你必须使用RegisterStartupScript而不是RegisterClientScriptBlock
Here My Example.
这里是我的例子。
MasterPage:
母版页:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs"
Inherits="prueba.MasterPage" %>
<!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>
<script type="text/javascript">
function confirmCallBack() {
var a = document.getElementById('<%= Page.Master.FindControl("ContentPlaceHolder1").FindControl("Button1").ClientID %>');
alert(a.value);
}
</script>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
WebForm1.aspx
WebForm1.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs" Inherits="prueba.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Content>
WebForm1.aspx.cs
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace prueba
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "js", "confirmCallBack();", true);
}
}
}
回答by Humberto
Is Button1visible? I mean, from the server side. Make sure Button1.Visible is true.
是Button1可见的?我的意思是,从服务器端。确保 Button1.Visible 为真。
Controls that aren't Visiblewon't be rendered in HTML, so although they are assigned a ClientID, they don't actually exist on the client side.
不是的控件不会Visible在 HTML 中呈现,因此尽管它们被分配了ClientID,但它们实际上并不存在于客户端。

