禁用主页面加载的usercontrol

时间:2008-12-11 17:10:47

标签: asp.net

我在某些页面上使用母版页。该母版页正在加载用户控件。所以我想在一些具有母版页的页面加载上禁用或启用用户控件。


无论如何我可以在母版页Page_load()

上禁用用户控件
<div class="ucTabCtrl" >
    <uc1:TLTabControl ID="ctrlname" runat="server" Visible="False" />
</div>

Master Page_load()
{
 // checking some condition if true
 ctrlname.visible = true;
}

但问题是我无法获取用户ctrl的实例,总之ctrlname始终为null。

3 个答案:

答案 0 :(得分:1)

你的问题有点难以理解,但我认为你所寻找的是这样的:

public partial class Site1 : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page is WebForm1 || Page is WebForm2)
        {
            webUserControl11.Visible = false;
        }
    }
}

或者,您可以在页面上实现指示此行为的接口。一些事情:

public partial class Site1 : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ISpecialPage specialPage = Page as ISpecialPage;

        if (specialPage != null && specialPage.ShouldDisableUserControl)
            webUserControl11.Visible = false;
    }
}

public interface ISpecialPage
{
    bool ShouldDisableUserControl { get; }
}

答案 1 :(得分:0)

您想在子页面上禁用它吗?您可以在Page_Load()方法中执行以下操作:

if (null != this.Master)
{
    userControl.Enabled = false;
}

答案 2 :(得分:0)

用户控件未在MasterPage中声明为本地变量。您需要使用FindControl()函数来获取对控件的引用。

这是一个有效的例子:

Dim userControl As WebControl = ContentPlaceHolder1.FindControl("someControl")
If userControl IsNot Nothing Then
   CType(userControl, WebControl).Enabled = False
End If