views:

18

answers:

1

How can I hide a user control on the master page from a content page? This is code I have on my content page's load.

    Dim banner As UserControl = DirectCast(Master.FindControl("uc_banner1"), UserControl)
    banner.Visible = True

Does nothing for me :(

+1  A: 

Expose the visible property of the user control via a property of the MasterPage.

In your MasterPage:

public bool MyBannerVisibility
{
    get { return uc_banner1.Visible; }
    set { uc_banner1.Visible = value; }
}

Then make sure to add a reference in your content page:

<%@ MasterType TypeName="YourMasterPageTypeName" %>

Then in your content page just do:

Master.MyBannerVisibility = false;

Edit: Since your using VB.net I used a code converter to convert it for you:

Public Property MyBannerVisibility() As String
    Get
        Return uc_banner1.Visible
    End Get
    Set
        uc_banner1.Visible = value
    End Set
End Property

Content Page:

Master.MyBannerVisibility = False
Kelsey
My MasterPage name is 'Site.Master' I only need to put 'Site' in the reference on the content page correct?
Landmine
@Landmine No you should just need `this.Master` (not sure what you mean by `Site.Master`. The `MasterType` definition in your content page will set `this.Master` to refer to your specific MasterPage.
Kelsey
The file name for my master page is "Site.Master"
Landmine
Thanks, you pretty much did all the work for me!
Landmine