By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,625 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,625 IT Pros & Developers. It's quick & easy.

update master page variable value from the content page by javascript.

P: 42
hi all, thanks in advance. I need a help , its urgent.

I am developing a webapplication with maser page. there i have a lebel, control. i need to update its value from the content page, and only by the javadscript.

i am doing:

var btnpjt = document.getElementById( '<%=((Label)this.Master.FindControl("lblBtnProject Value")).ClientID %>');
btnpjt.value='VP';
in my content page content page. But it is not working.
Depending on the Value stored by the Javascript in the master page label, i have to redirect my content page to the another page.

The second question is, how could i initialize(to NULL) my master page value from the content page .

Please reply , i have urgency.
Feb 5 '10 #1
Share this Question
Share on Google+
3 Replies


Frinavale
Expert Mod 5K+
P: 9,731
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)
Expand|Select|Wrap|Line Numbers
  1.  CType(Master.FindControl("myLabel"), Label).Text = "the text to display"
  2.  
(C#)
Expand|Select|Wrap|Line Numbers
  1.  (Label) Master.FindControl("myLabel").Text = "the text to display";
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. Public ReadOnly Property LabelClientID As String
  2.   Get
  3.    return Ctype(Master.FindControl("lblBtnProject"),String).ClientID
  4.   End Get
  5. End Property
C#
Expand|Select|Wrap|Line Numbers
  1.     public string LabelClientID 
  2.     {
  3.         get { 
  4.          return  (Label) Master.FindControl("lblBtnProject").ClientID;
  5.         }
  6.     }
Then in your JavaScript (in your ASPX page):
Expand|Select|Wrap|Line Numbers
  1. var btnpjt = document.getElementById( '<%=LabelClientID %>');
  2.  

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( '<%
Expand|Select|Wrap|Line Numbers
  1. If( (Label) Master.FindControl("lblBtnProject") != null)
  2. { (Label)this.Master.FindControl("lblBtnProject")).ClientID}
  3. else{""}
  4. %>');
Or even simplify it to:
Expand|Select|Wrap|Line Numbers
  1. var btnpjt = document.getElementById( '<%=(String)Master.FindControl("lblBtnProject")!= null ? Master.FindControl("lblBtnProject").ClientID: "") %>');
  2. ...
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:
Expand|Select|Wrap|Line Numbers
  1. var btnpjt = document.getElementById( '<%=(String)Master.FindControl("lblBtnProject")!= null ? Master.FindControl("lblBtnProject").ClientID: "") %>');
  2.  
  3. //checks to make sure that btnpjt is not null/nothing
  4. if(btnpjt)
  5. {
  6. }
  7.  
-Frinny
Feb 8 '10 #2

P: 42
hi Frinny, thank u for ur reply. But when i am using the property as described u in my aspx.cs page i am getting error as
"Error

"Cannot convert type 'string' to 'System.Web.UI.WebControls.Label'
Feb 10 '10 #3

Frinavale
Expert Mod 5K+
P: 9,731
Try creating the Public Property that returns the string.
Feb 10 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.