Accessing controls in Master page from content page

user3411907 picture user3411907 · Mar 12, 2014 · Viewed 14.3k times · Source

I am trying to access control in Master page from Content page(Asp.net) using javascript like this

alert(document.getElementById('<%=((Label)Master.FindControl("lbl")).ClientID %>').value);

control in Master page is as follow,

 <asp:Label ID="lbl" runat="server" Text="one"></asp:Label>

But unfortunately it is not working. I am getting undefined value

Answer

juan.facorro picture juan.facorro · Mar 12, 2014

I noticed that you are actually accessing the .value field of the element that the <asp:Label /> control generates, which is a <span></span>. This type of element won't return anything for the .value attribute. If you are actually trying to access its text then use:

alert(document.getElementById('<%=((Label)Master.FindControl("lbl")).ClientID %>').innerText);

or

alert(document.getElementById('<%=((Label)Master.FindControl("lbl")).ClientID %>').innerHTML);