tags:

views:

1350

answers:

5

I am using master page on some pages. And that master page is loading the user control. So I want to disable or enable user control on some page load which has master page.

A: 

You want to disable it on the child page? You could do something like this in the Page_Load() method:

if (null != this.Master)
{
    userControl.Enabled = false;
}
Steven Behnke
Is there anyway can I disable User control on master page Page_load()
alice7
You'd probably have to use FindControl() from the context of the content placeholder and look for a particular user control.
Steven Behnke
A: 

Is there anyway can I disable User control on master page Page_load()

alice7
+1  A: 

Your question is kinda hard to understand, but i think what you are looking for is something like this:

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

Alternatively you could implement an interface on the pages that indicates this behavior. Something along the lines:

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; }
}
Tom Jelen
A: 

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

but the problem is I m not able to get the instance of user ctrl , in short ctrlname is null all the time.

alice7
If the UserControl is added on the MasterPage through the markup (or the designer), then it should be available at Page_Load in the MasterPage.Are you adding the UserControl dynamically to the MasterPage?
Tom Jelen
A: 

The user control is not declared as a local variable in the MasterPage. You need to use the FindControl() function to get a reference to the control.

Here's a working example:

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