可以将用户控件隐藏在内容页的母版页中吗?

时间:2022-06-13 08:08:48

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 个解决方案

#1


2  

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

通过母版的属性公开用户控件的可见属性。

In your MasterPage:

在你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:

编辑:由于您使用VB.net,我使用了一个代码转换器为您转换:

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

#1


2  

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

通过母版的属性公开用户控件的可见属性。

In your MasterPage:

在你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:

编辑:由于您使用VB.net,我使用了一个代码转换器为您转换:

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