2008-12-11 92 views
2

我在一些頁面上使用母版頁。並且該母版頁正在加載用戶控件。所以我想禁用或啓用用戶控制一些頁面加載有主頁面。在主頁面加載禁用usercontrol


反正是有,我可以在母版頁的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始終爲空。

+0

你的問題是完全不清楚。您需要詳細說明並描述您的頁面設置,並說明您希望禁用哪些頁面。如果可能,請提供簡化的代碼示例。 – Bryan 2008-12-12 00:50:59

回答

0

你想在子頁面上禁用它嗎?你可以做這樣的事情在Page_Load()方法:

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

是否有我可以禁用用戶控制的母版頁Page_load() – alice7 2008-12-11 17:19:46

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; } 
} 
0

用戶控件沒有聲明爲MasterPage中的局部變量。您需要使用FindControl()函數來獲取對該控件的引用。

這裏有一個工作示例:

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