Sign in  
Disclaimer =  About us 
 
      

Extender provider components in ASP.NET

Using the component

The source code file ExtenderProvider.cs declares a base component ExtenderProviderComponent and a support class ExtenderProviderEngine. If your extender provider is a component, derive a component from ExtenderProviderComponent and add the ProvideProperty attributes:

[ProvideProperty ("DoMagic", typeof(System.Web.UI.Control))]
public class MyProvider : GoodHeavens.ComponentModel.ExtenderProviderComponent
{

The properties declared by a ProvideProperty attribute must be serializable. All native .NET types and enumerations are serializable; more complex types must implement the ISerializable interface. Note that you cannot add two ProvideProperty attributes for the same property with different types - only the top one is seen by Visual Studio - this is also an undocumented "feature".

The base class ExtenderProviderComponent already implements the IExtenderProvider interface, allowing only web controls to be extended. If you want to restrict that even further, override the CanExtendControl method:

  protected override bool CanExtendControl (System.Web.UI.Control AExtendee)
  {
     // Your logic goes here; return true to allow extension
  }
 

The base class provides storage for properties. Every property has a (unique) name. Use that in the Get/Set functions for the provided properties: 

  public bool GetDoMagic (System.Web.UI.Control AExtendee)
  {
    // GetControlPropertyValue arguments:   
    // control to extend, property name, default value
    return (bool )Engine.GetControlPropertyValue
      (AExtendee,        "DoMagic",      false);
  }

  public void SetDoMagic (System.Web.UI.Control AExtendee, bool AYes)
  {
    // SetControlPropertyValue arguments:
    //   control to extend, property name, value, isdefault
    Engine.SetControlPropertyValue
        (AControl,          "DoMagic",     AYes,  !AYes);
    // isdefault is a boolean that indicates whether the value
    // is the default value.
  }

At run-time you need to hook into events for the page generation process; override the InitialiseHandlers method:

  protected override void InitialiseHandlers ()
  {
    foreach (System.Web.UI.Control c
                    in Engine.ControlsWithValue ("DoMagic")
    {
      c.PreRender += new EventHandler (MagicStuff);
    }
  }

And finally you have to add the event handlers that implement all that magical stuff your component is famous for:

  private void MagicStuff (object sender, EventArgs e)
  {
    // Here the magic happens
  }
}

Add your component to a class library, add the component to the Toolbox and you're ready to go. Values for the provided properties can be entered via the Viasual Studio IDE for all web controls except the page or UserControl you are designing (the "this" in the code behind). If you need to do that, add a line of code to the Page_Load method by hand:

  private void Page_Load (object sender, System.EventArgs e)
  {
    MyProvider1.SetDoMagic (this, true);
  } 

Contents

Downloads

Also available on  CodeProject
26 july 2004