Dave Burke : Freelance .NET Web Developer specializing in Online Communities

Referencing a Parent Property from a UserControl with Intellisense

This is my .NET tidbit of the day.  I am driven to help the 3 developers out there who don't yet know how to do this.  I'm also a big fan (fascinaciature, really) of how .NET references objects across controls, in parent pages, or via reflection to outside an application domain. 

So SomeParent contains a boolean property...

public class SomeParentPage : System.Web.UI.Page
{
 public bool blnSomething
 {
  get
  {
   try
   {
    return (bool) ViewState["blnSomething"];
   }
   catch
   {
    return true;
   }
  }
  set
  {
   ViewState["blnSomething"] = value;
  }
 }
}  


To reference blnSomething in a UserControl, you can reference it once where you might need it, of course, or can create a property and reference it throughout the control.  The trick is to cast the Parent.Page object into the class name of the page, where you can dot.reference the property with Intellisense. 


public abstract class SomeUserControl: System.Web.UI.UserControl
{
 public bool blnSomething
 {
  get
  {
   return ((SomeParentPage) Parent.Page).blnSomething;
  }
 }
}

Comments (0) | Post RSS RSS comment feed

Posted on 12/1/2004 2:01:00 PM by Dave Burke
Categories: .NET
Tags:

Related posts


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke