473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
3 9290
Frinavale
9,735 Expert Mod 8TB
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
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
9,735 Expert Mod 8TB
Try creating the Public Property that returns the string.
Feb 10 '10 #4

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

Similar topics

0
by: David | last post by:
In ASP.NET 2.0, is it possible to use a control that's on a Master Page as a select parameter in a content page's datasource control? For example, the Master Page has a drop down list of...
2
by: Learner | last post by:
Hello, This is first time I have started using Master Pages. I am using ASP.NET 2.0 (VB) in Visual Studion 2005. This is how I am doing it... I have created a master page and placed a...
5
by: Paul | last post by:
I have a MasterPage that has on it several UserControls (*ascx files). I wish to control the visibility of the controls on the content pages according to some parameter (for example on a user...
5
by: Aussie Rules | last post by:
Hi, Within the code of a content page, how do you say set the .text value of a label thats on the master page.... Thanks
3
by: Managed Code | last post by:
I have a BasePage class that derives from System.Web.UI.Page. All of my content pages derive from this. The derived page classes use the following MasterType declaration that follows to reference...
2
by: gomzi | last post by:
hi, I would like to know as to how I could access the body tag in the master page through a content page. I require this functionality for calling some javascript functions dynamically through the...
1
by: Dinu | last post by:
How to Dynamically program User controls in a Master page from a content page Thanks
1
by: pierre | last post by:
Hi, I have a DataList inside a UserControl, which I use on a MasterPage. My ContentPage lists items which a user can add to his/her shopping basket. Can someone PLEASE let me know how I can...
2
by: Shailesh Patel | last post by:
Hi, I havea variable in cotent page. I want to read that variable into usercontrol located in master page. Is there any trick to read it in usercontrol? Thank you in advance. Shailesh
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.