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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sweetneel
    New Member
    • Oct 2008
    • 42

    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.getEle mentById( '<%=((Label)thi s.Master.FindCo ntrol("lblBtnPr ojectValue")).C lientID %>');
    btnpjt.value='V P';
    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.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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)
    Code:
     CType(Master.FindControl("myLabel"), Label).Text = "the text to display"
    (C#)
    Code:
     (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:
    Code:
    Public ReadOnly Property LabelClientID As String
      Get
       return Ctype(Master.FindControl("lblBtnProject"),String).ClientID
      End Get
    End Property
    C#
    Code:
        public string LabelClientID 
        {
            get { 
             return  (Label) Master.FindControl("lblBtnProject").ClientID;
            }
        }
    Then in your JavaScript (in your ASPX page):
    Code:
    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 "lblBtnProj ect Value" but it should just be "lblBtnProject" )

    The Null/Nothing problem can easily be solved:
    var btnpjt = document.getEle mentById( '<%
    Code:
    If( (Label) Master.FindControl("lblBtnProject") != null)
    { (Label)this.Master.FindControl("lblBtnProject")).ClientID}
    else{""}
    %>');
    Or even simplify it to:
    Code:
    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:
    Code:
    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

    Comment

    • sweetneel
      New Member
      • Oct 2008
      • 42

      #3
      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.Lab el'

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Try creating the Public Property that returns the string.

        Comment

        Working...