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
2008-12-11 17:15:07
Is there anyway can I disable User control on master page Page_load()
alice7
2008-12-11 17:19:46
You'd probably have to use FindControl() from the context of the content placeholder and look for a particular user control.
Steven Behnke
2008-12-11 19:52:56
+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
2008-12-11 19:24:22
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
2008-12-11 21:10:09
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
2008-12-11 21:22:57
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
2009-01-10 03:02:30