[javascript] ASP.NET document.getElementById('<%=Control.ClientID%>'); returns null

I'm trying to retrieve a server control in JavaScript. For testing purposes I am calling the JavaScript function from the page load event.

protected void Page_Load(object sender, EventArgs e){
    ClientScript.RegisterClientScriptBlock(GetType(), "js", "confirmCallBack();", true);
}

And my JavaScript function is

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.

I should add that I'm using master page.

Any ideas.

This question is related to javascript asp.net

The answer is


Is Button1 visible? I mean, from the server side. Make sure Button1.Visible is true.

Controls that aren't Visible won't be rendered in HTML, so although they are assigned a ClientID, they don't actually exist on the client side.