Problem using Label in MasterPage

Last post 05-12-2008 4:35 AM by basquepower. 11 replies.

Sort Posts:

  • Problem using Label in MasterPage

    04-22-2008, 9:44 AM
    • Loading...
    • basquepower
    • Joined on 02-15-2008, 3:45 AM
    • Bilbao (Basque Country)
    • Posts 156

     Hello folks,

    This is my problem. I have put a Label (it is called Label1) in my Masterpage and I´ve configured as not visible at the beginning. I have a file called "ChooseNumber.aspx". In this file there´s button and when is pressed I want to make the label visible first and also give a value to it. But in "ChooseNumber.aspx" file I don´t have access to this component because they are in different programs. What advice to you give me to solve my problem?

    Thank you very much. 

  • Re: Problem using Label in MasterPage

    04-22-2008, 10:00 AM
    Answer
    • Loading...
    • shayas
    • Joined on 02-06-2008, 12:42 PM
    • Sharjah,UAE
    • Posts 221

    expose the label as public property in the master apge and then

    add the below to content aspx

    <%@ MasterType VirtualPath="[urmasterpage path]" %>

    and then u can access label using

    Master.urlabelproperty

     

    Yasser
    R&D Dept.
    Industry Networks.

    Please remember to mark as answers if this helps
  • Re: Problem using Label in MasterPage

    04-22-2008, 10:16 AM
    • Loading...
    • basquepower
    • Joined on 02-15-2008, 3:45 AM
    • Bilbao (Basque Country)
    • Posts 156

     Thanks for the information.

    Just a question: how do I make the label public?
     

  • Re: Problem using Label in MasterPage

    04-22-2008, 11:17 AM
    Answer
    • Loading...
    • hermiod
    • Joined on 08-30-2007, 12:31 PM
    • Posts 76

    I believe Shavas meant create a public property accessible from the content page which can then be used to alter the text of the private label. Something like this:

    Public Property MasterLabel() as string
    Get
      Return
    MyLabel.Text
    End Get
    Set
    (ByVal value As String)
      Mylabel.Text=Value
      MyLabel.Visible=True
    End Set
    End Property

     You can then access this property from your Content page like so.

    Master.MasterLabel = "Whatever text I want to put in my label"

     

    Hope this solves your problem.

    Please Mark As Answer if this post helped you.
  • Re: Problem using Label in MasterPage

    05-09-2008, 9:34 AM
    • Loading...
    • basquepower
    • Joined on 02-15-2008, 3:45 AM
    • Bilbao (Basque Country)
    • Posts 156

    hermiod:

    I believe Shavas meant create a public property accessible from the content page which can then be used to alter the text of the private label. Something like this:

    Public Property MasterLabel() as string
    Get
      Return
    MyLabel.Text
    End Get
    Set
    (ByVal value As String)
      Mylabel.Text=Value
      MyLabel.Visible=True
    End Set
    End Property

     You can then access this property from your Content page like so.

    Master.MasterLabel = "Whatever text I want to put in my label"

     

    Hope this solves your problem.

     

     

    I´ve tried to write your code but doesn´t recognize the function. I´ve written  <%@ MasterType VirtualPath="~/AppMaster.master"%> in my aspx file. I´ve written the code above in MasterPage.master.cs file in this way:

    public string MasterLabel
        {
            get {return Label1.Text;}
            set {Label1.Visible=true; Label1.Text="parameter that this function receives";}
        }

    I want to pass an argument to this function (the text the label will show) but oesn´t accept any argument and if I write an argument get and set properties are not recognized.

    No idea what´s wrong. Please help!
     

  • Re: Problem using Label in MasterPage

    05-09-2008, 11:39 AM
    • Loading...
    • hermiod
    • Joined on 08-30-2007, 12:31 PM
    • Posts 76

    I'm not familiar with C# so this may be the same as what you've already got but try this...

     

    public string MasterLabel {
        get { return MyLabel.Text; }
        set {
            Mylabel.Text = value;
            MyLabel.Visible = true;
        }
    }

     Its a direct conversion from the code I posted.

    I don't know if there is something different between C# and VB.Net but you should be able to just do Master.MasterLabel = "hello jello". Perhaps a C# expert could weigh in with a little help if this doesn't solve it...?

    EDIT: Try adding the following into your @page directive as well as the @MasterType you already have..

     MasterPageFile="~/Reports.master"

    It's straight after Language in mine.

    Please Mark As Answer if this post helped you.
  • Re: Problem using Label in MasterPage

    05-09-2008, 11:54 AM
    • Loading...
    • basquepower
    • Joined on 02-15-2008, 3:45 AM
    • Bilbao (Basque Country)
    • Posts 156

     Hi hermiod,

    hermiod:

    EDIT: Try adding the following into your @page directive as well as the @MasterType you already have..

     MasterPageFile="~/Reports.master"

    It doesn´t recognize MasterPageFile in @page directory.

    Anyway, thank you very much hermiod. I hope a c# expert will help me with this.

     

  • Re: Problem using Label in MasterPage

    05-09-2008, 11:56 AM
    • Loading...
    • basquepower
    • Joined on 02-15-2008, 3:45 AM
    • Bilbao (Basque Country)
    • Posts 156

    basquepower:
    It doesn´t recognize MasterPageFile in @page directory.
     

    Sorry I wanted to say that doesn´t recognize Report.master in MasterPageFile . 

  • Re: Problem using Label in MasterPage

    05-09-2008, 4:50 PM
    • Loading...
    • hermiod
    • Joined on 08-30-2007, 12:31 PM
    • Posts 76

    Sorry I should've been clearer on that, Change 'Reports.master' to the name of your master file.

    Please Mark As Answer if this post helped you.
  • Re: Problem using Label in MasterPage

    05-12-2008, 3:03 AM
    • Loading...
    • basquepower
    • Joined on 02-15-2008, 3:45 AM
    • Bilbao (Basque Country)
    • Posts 156

     Any idea of how to access to a Masterpage component and change its value? The post was written as Resolved but it isn´t resolved yet.

  • Re: Problem using Label in MasterPage

    05-12-2008, 4:03 AM
    • Loading...
    • hermiod
    • Joined on 08-30-2007, 12:31 PM
    • Posts 76

    Step by step, this is what I've done in my pages which works. I have followed these instructions myself after doing them and everything worked fine, so this should do the trick.

     

    Step 1: Add this to the @Page directive in your content page where 'Reports.master' is the name of your master page.

    MasterPageFile="~/Reports.master"

    Step 2: Add this below the @Page directive in your content page where 'Reports.master' is the name of your master page.

    <%@ MasterType VirtualPath="~/Reports.master" %>

    Step 3: Add this after your class and 'inherits' statement but before your first sub (this isn't required, but is just good practice to keep properties at the top of the page.)

    Public Property MasterLabel() As String

    Get

    Return MyLabel.Text

    End Get

    Set(ByVal value As String)

    Mylabel.Text = Value

    MyLabel.Visible = True

    End Set

    End Property

    Step 4: Add master.MasterLabel = "Whatever I want it to say" to an event in one of your content pages, Page_Load is best for testing purposes.

    Step 5: Rebuild your website - after ensuring that there is a label control called MyLabel in the master page. The text you assigned to the MasterLabel property should appear in the label control on your master page.

     

    If this still doesn't work, I will post the complete code from my sample project.

    Please Mark As Answer if this post helped you.
  • Re: Problem using Label in MasterPage

    05-12-2008, 4:35 AM
    • Loading...
    • basquepower
    • Joined on 02-15-2008, 3:45 AM
    • Bilbao (Basque Country)
    • Posts 156

    Thank you very much hermiod for your help and patience. Finally I could write to the Label which is in MasterPage. I´ve done step by step what you have told me.

    The main problem was in the function which is in masterpage.master.cs to change the value of the Label. I´m going to write the final solution for anyone who may have my problem. 

    I´m going to write your same code but in c#.

    Step 1: Add this to the @Page directive in your content page where 'Reports.master' is the name of your master page.

    MasterPageFile="~/Reports.master"

    Step 2: Add this below the @Page directive in your content page where 'Reports.master' is the name of your master page.

    <%@ MasterType VirtualPath="~/Reports.master" %>

    Step 3: Add this after your class and 'inherits' statement but before your first sub (this isn't required, but is just good practice to keep properties at the top of the page.)

    public string MasterLabel
        {
            get
            {
                return Label1.Text;
            }
            set
            {
                Label1.Text = value;
                Label1.Visible=true;
            }
        }

    Step 4: Add master.MasterLabel = "Whatever I want it to say" to an event in one of your content pages, Page_Load is best for testing purposes.

    Step 5: Rebuild your website - after ensuring that there is a label control called MyLabel in the master page. The text you assigned to the MasterLabel property should appear in the label control on your master page.

    I hope the post will be helpful for many people.

    Thank you, one more time, hermiod. You deserve going to the Heaven Smile

    Bye. 

     

Page 1 of 1 (12 items)