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.
Gotcha!
You have to use
RegisterStartupScriptinstead ofRegisterClientScriptBlockHere My Example.
MasterPage:
WebForm1.aspx
WebForm1.aspx.cs