You don't need to use JavaScript to accomplish this task.
You can reference a control on the MasterPage using the MasterPage's FindControl method.
For example, if you want to set the text value of a Label that is located in the MasterPage from a content page then you would have something like this:
(VB.NET)
-
CType(Master.FindControl("myLabel"), Label).Text = "the text to display"
-
(C#)
-
(Label) Master.FindControl("myLabel").Text = "the text to display";
-
If you must updated it using JavaScript (this doesn't make sense) then I would recommend that you make a Protected/Public Property in the content page that exposes the ClientID of the Label in the MasterPage to your ASP code.
For example:
In your VB.NET code you would have a Public Property:
-
Public ReadOnly Property LabelClientID As String
-
Get
-
return Ctype(Master.FindControl("lblBtnProject"),String).ClientID
-
End Get
-
End Property
C#
-
public string LabelClientID
-
{
-
get {
-
return (Label) Master.FindControl("lblBtnProject").ClientID;
-
}
-
}
Then in your JavaScript (in your ASPX page):
-
var btnpjt = document.getElementById( '<%=LabelClientID %>');
-
Actually when I was copy/pasting your code I think I fo und the problem.
You aren't locating the control properly because you aren't using the ID for the lblBtnProject control (you have "lblBtnProject Value" but it should just be "lblBtnProject")
The Null/Nothing problem can easily be solved:
var btnpjt = document.getElementById( '<%
- If( (Label) Master.FindControl("lblBtnProject") != null)
-
{ (Label)this.Master.FindControl("lblBtnProject")).ClientID}
-
else{""}
-
%>');
Or even simplify it to:
- var btnpjt = document.getElementById( '<%=(String)Master.FindControl("lblBtnProject")!= null ? Master.FindControl("lblBtnProject").ClientID: "") %>');
-
...
In your JavaScript code you should always check to make sure that the element you're working with exists before you use it. For example:
- var btnpjt = document.getElementById( '<%=(String)Master.FindControl("lblBtnProject")!= null ? Master.FindControl("lblBtnProject").ClientID: "") %>');
-
-
//checks to make sure that btnpjt is not null/nothing
-
if(btnpjt)
-
{
-
}
-
-Frinny