Changing a control on a masterpage that uses a second masterpage

Discussion in 'ASP .Net' started by Torben Laursen, Mar 14, 2008.

  1. I want to change the label of a component that I have on a masterpage but I
    cannot get my hands on it
    I have a mastepage MP2 that has a button called B2
    masterpage MP2 uses masterpage MP1 that has a button called B1

    I then have a page called default and in the page load I tryed:
    System.Web.UI.WebControls.Button B10 =
    (System.Web.UI.WebControls.Button)Master.FindControl("B1");
    System.Web.UI.WebControls.Button B20 =
    (System.Web.UI.WebControls.Button)Master.FindControl("B2");

    However both B10 and B20 is null.

    So how do I get my hands on B1 and B2 so I can set there label?

    Thanks Torben
     
    Torben Laursen, Mar 14, 2008
    #1
    1. Advertising

  2. Torben Laursen

    clintonG Guest

    I haven't used --nested-- Master Pages yet but they must still compile into
    the same control tree as any other page. I've had insight by enabling
    trace="true" on the page and then looking for the control I wanted to
    reference in the control tree. Then its a matter of using the FindControl
    method which gets ugly as it often has to be used repeatedly in the same
    statement. That is what is called late binding.

    You can try early binding by developing a Property for the label control
    which allows direct access to the control by referencing its property.
    Here's some notes I copied out of a file...

    //A master page can expose properties by simply making
    //those properties public within the master page.

    //private string _someProperty;
    //public string SomeProperty
    //{
    // get { return _someProperty; }
    // set { _someProperty = value; }
    //}


    "Torben Laursen" <> wrote in message
    news:...
    >I want to change the label of a component that I have on a masterpage but I
    >cannot get my hands on it
    > I have a mastepage MP2 that has a button called B2
    > masterpage MP2 uses masterpage MP1 that has a button called B1
    >
    > I then have a page called default and in the page load I tryed:
    > System.Web.UI.WebControls.Button B10 =
    > (System.Web.UI.WebControls.Button)Master.FindControl("B1");
    > System.Web.UI.WebControls.Button B20 =
    > (System.Web.UI.WebControls.Button)Master.FindControl("B2");
    >
    > However both B10 and B20 is null.
    >
    > So how do I get my hands on B1 and B2 so I can set there label?
    >
    > Thanks Torben
     
    clintonG, Mar 14, 2008
    #2
    1. Advertising

  3. Thanks,

    I tryed this. I made a property on both master pages so I can access the 2
    buttons.

    But using this method I can only get MP2.

    So I still need a way to access both B1 and B2

    Torben

    "clintonG" <> wrote in message
    news:...
    >I haven't used --nested-- Master Pages yet but they must still compile into
    >the same control tree as any other page. I've had insight by enabling
    >trace="true" on the page and then looking for the control I wanted to
    >reference in the control tree. Then its a matter of using the FindControl
    >method which gets ugly as it often has to be used repeatedly in the same
    >statement. That is what is called late binding.
    >
    > You can try early binding by developing a Property for the label control
    > which allows direct access to the control by referencing its property.
    > Here's some notes I copied out of a file...
    >
    > //A master page can expose properties by simply making
    > //those properties public within the master page.
    >
    > //private string _someProperty;
    > //public string SomeProperty
    > //{
    > // get { return _someProperty; }
    > // set { _someProperty = value; }
    > //}
    >
    >
    > "Torben Laursen" <> wrote in message
    > news:...
    >>I want to change the label of a component that I have on a masterpage but
    >>I cannot get my hands on it
    >> I have a mastepage MP2 that has a button called B2
    >> masterpage MP2 uses masterpage MP1 that has a button called B1
    >>
    >> I then have a page called default and in the page load I tryed:
    >> System.Web.UI.WebControls.Button B10 =
    >> (System.Web.UI.WebControls.Button)Master.FindControl("B1");
    >> System.Web.UI.WebControls.Button B20 =
    >> (System.Web.UI.WebControls.Button)Master.FindControl("B2");
    >>
    >> However both B10 and B20 is null.
    >>
    >> So how do I get my hands on B1 and B2 so I can set there label?
    >>
    >> Thanks Torben

    >
     
    Torben Laursen, Mar 14, 2008
    #3
  4. Hi,

    Let me see if I understood your problem

    You have a scenario like this:

    +------------
    | Master Page 1
    |
    | +-----------
    | | Master Page 2
    | |
    | | +----------
    | | | Content Page
    | | |

    And you're trying to access from Content Page a control on the Master
    Page 1. Am I correct?

    If so, I think you can do the following

    Private Sub ContentPage_Load(sender as Object, e as EventArgs) Handles
    Me.Load

    Dim mp2 As MasterPage = Me.Master
    Dim mp1 As MasterPage = mp2.Master

    Dim btn1 As Button = CType(mp1.FindControl("btnMaster1"),
    Button)
    Dim btn2 As Button =
    CType(mp1.FindControl("ContentPlaceHolder1").FindControl("btnMaster2"),
    Button)

    btn1.Text = "MP1 Changed from Content Page"
    btn2.Text = "MP2 Changed from Content Page"

    End Sub

    I've put up a sample test project at http://rapidshare.com/files/99551862/NestedMasterPage.zip.html

    Anyways, the problem is that when using nested Master Pages, the
    hierarchy is created on the top most Master Page.

    And as you can see by the code snippet the ContentPlaceHolder is build
    into the hierarchy.

    Regards,

    Paulo Santos
    http://pjondevelopment.50webs.com


    On Mar 14, 11:31 am, "Torben Laursen" <> wrote:
    > Thanks,
    >
    > I tryed this. I made a property on both master pages so I can access the 2
    > buttons.
    >
    > But using this method I can only get MP2.
    >
    > So I still need a way to access both B1 and B2
    >
    > Torben
    >
    > "clintonG" <> wrote in message
    >
    > news:...
    >
    >
    >
    > >I haven't used --nested-- Master Pages yet but they must still compile into
    > >the same control tree as any other page. I've had insight by enabling
    > >trace="true" on the page and then looking for the control I wanted to
    > >reference in the control tree. Then its a matter of using the FindControl
    > >method which gets ugly as it often has to be used repeatedly in the same
    > >statement. That is what is called late binding.

    >
    > > You can try early binding by developing a Property for the label control
    > > which allows direct access to the control by referencing its property.
    > > Here's some notes I copied out of a file...

    >
    > > //A master page can expose properties by simply making
    > > //those properties public within the master page.

    >
    > > //private string _someProperty;
    > > //public string SomeProperty
    > > //{
    > > //    get { return _someProperty; }
    > > //    set { _someProperty = value; }
    > > //}

    >
    > > "Torben Laursen" <> wrote in message
    > >news:...
    > >>I want to change the label of a component that I have on a masterpage but
    > >>I cannot get my hands on it
    > >> I have a mastepage MP2 that has a button called B2
    > >> masterpage MP2 uses masterpage MP1 that has a button called B1

    >
    > >> I then have a page called default and in the page load I tryed:
    > >>        System.Web.UI.WebControls.Button B10 =
    > >> (System.Web.UI.WebControls.Button)Master.FindControl("B1");
    > >>        System.Web.UI.WebControls.Button B20 =
    > >> (System.Web.UI.WebControls.Button)Master.FindControl("B2");

    >
    > >> However both B10 and B20 is null.

    >
    > >> So how do I get my hands on B1 and B2 so I can set there label?

    >
    > >> Thanks Torben- Hide quoted text -

    >
    > - Show quoted text -
     
    PJ on Development, Mar 14, 2008
    #4
  5. Torben Laursen

    clintonG Guest

    Which is why enabling trace is so useful for referencing controls.

    <%= Clinton Gallagher


    "PJ on Development" <> wrote in message
    news:...
    Hi,

    Let me see if I understood your problem

    You have a scenario like this:

    +------------
    | Master Page 1
    |
    | +-----------
    | | Master Page 2
    | |
    | | +----------
    | | | Content Page
    | | |

    And you're trying to access from Content Page a control on the Master
    Page 1. Am I correct?

    If so, I think you can do the following

    Private Sub ContentPage_Load(sender as Object, e as EventArgs) Handles
    Me.Load

    Dim mp2 As MasterPage = Me.Master
    Dim mp1 As MasterPage = mp2.Master

    Dim btn1 As Button = CType(mp1.FindControl("btnMaster1"),
    Button)
    Dim btn2 As Button =
    CType(mp1.FindControl("ContentPlaceHolder1").FindControl("btnMaster2"),
    Button)

    btn1.Text = "MP1 Changed from Content Page"
    btn2.Text = "MP2 Changed from Content Page"

    End Sub

    I've put up a sample test project at
    http://rapidshare.com/files/99551862/NestedMasterPage.zip.html

    Anyways, the problem is that when using nested Master Pages, the
    hierarchy is created on the top most Master Page.

    And as you can see by the code snippet the ContentPlaceHolder is build
    into the hierarchy.

    Regards,

    Paulo Santos
    http://pjondevelopment.50webs.com


    On Mar 14, 11:31 am, "Torben Laursen" <> wrote:
    > Thanks,
    >
    > I tryed this. I made a property on both master pages so I can access the 2
    > buttons.
    >
    > But using this method I can only get MP2.
    >
    > So I still need a way to access both B1 and B2
    >
    > Torben
    >
    > "clintonG" <> wrote in message
    >
    > news:...
    >
    >
    >
    > >I haven't used --nested-- Master Pages yet but they must still compile
    > >into
    > >the same control tree as any other page. I've had insight by enabling
    > >trace="true" on the page and then looking for the control I wanted to
    > >reference in the control tree. Then its a matter of using the FindControl
    > >method which gets ugly as it often has to be used repeatedly in the same
    > >statement. That is what is called late binding.

    >
    > > You can try early binding by developing a Property for the label control
    > > which allows direct access to the control by referencing its property.
    > > Here's some notes I copied out of a file...

    >
    > > //A master page can expose properties by simply making
    > > //those properties public within the master page.

    >
    > > //private string _someProperty;
    > > //public string SomeProperty
    > > //{
    > > // get { return _someProperty; }
    > > // set { _someProperty = value; }
    > > //}

    >
    > > "Torben Laursen" <> wrote in message
    > >news:...
    > >>I want to change the label of a component that I have on a masterpage
    > >>but
    > >>I cannot get my hands on it
    > >> I have a mastepage MP2 that has a button called B2
    > >> masterpage MP2 uses masterpage MP1 that has a button called B1

    >
    > >> I then have a page called default and in the page load I tryed:
    > >> System.Web.UI.WebControls.Button B10 =
    > >> (System.Web.UI.WebControls.Button)Master.FindControl("B1");
    > >> System.Web.UI.WebControls.Button B20 =
    > >> (System.Web.UI.WebControls.Button)Master.FindControl("B2");

    >
    > >> However both B10 and B20 is null.

    >
    > >> So how do I get my hands on B1 and B2 so I can set there label?

    >
    > >> Thanks Torben- Hide quoted text -

    >
    > - Show quoted text -
     
    clintonG, Mar 15, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QWxleCBNYWdoZW4=?=

    MasterPage -> Page -> MasterPage Code Access

    =?Utf-8?B?QWxleCBNYWdoZW4=?=, Apr 10, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    7,922
    Steven Cheng[MSFT]
    Apr 14, 2006
  2. ThunderMusic
    Replies:
    2
    Views:
    463
    Gregory Gadow
    Sep 27, 2006
  3. Randy Smith
    Replies:
    9
    Views:
    450
    Mark Rae
    Apr 14, 2007
  4. Joe Kovac
    Replies:
    1
    Views:
    417
    Alexey Smirnov
    Aug 3, 2007
  5. yelipolok
    Replies:
    4
    Views:
    274
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page