Generic ASP.NET 2.0 IExtenderProvider: Solution

Published Tue, Feb 21 2006 10:35 PM


A couple of months back, in my spare time, I was working on an ASP.NET IExtenderProvider for .NET 2.0 (Think it was in with the september CTP)

Found some articles on codeproject about the subject:
http://www.codeproject.com/aspnet/FixingIExtenderProvider.asp by Wouter van Vugt
http://www.codeproject.com/aspnet/ExtenderProviderComponent.asp by Frank Robijn

But I made it work with the DefaultButton Control of Andy Smith as an ulimate example.

A lot of code was needed to make an ASP.NET IExtenderProvider so i tried to make a Generic base class to make it easier to implement an ASP.NET IExtenderProvider.
Actually I like the amount of code needed in Windows Forms, so tried to achief that. Unfortunately I could not make the Generic version work...

This week I needed an ASP.NET IExtenderProvider again. Found the sources from my project of last year and I was suprised to see it is working with the RTM of VS.NET and a little change in the code.

In the mean time I read Nikhil Kothari's weblog about the Atlas ExtenderControl.
The Atlas version has almost the same functionality as my version has, but it supports renaming and deleting controls from a webform.
The Atlas version is not suitable to work with standard ASP.NET, so with a little effort I added the missing functionality in my Generic version too.
Besides this I renamed my classes so they match the Atlas ExtenderControl.

The ExtenderControl I made is based on the DefaultButton Controls of Andy Smith. The Atlas version has a quite different solution.
The Designer is the IExtenderControl in stead of the ExtenderControl itself for example. Because of this no custom Designer is needed when you use my ExtenderControl.

Anyway, now you can make an ASP.NET 2.0 IExtenderProvider with a minimal effort.
When making a IExtenderProvider yourself you have to follow the next 2 steps:

Step 1: Defining the Extender properties :

    9 public class ValidationProperties : TargetControlProperties

   10     {

   11         [NotifyParentProperty(true)]

   12         public bool ValidationEnabled

   13         {

   14             get

   15             {

   16                 object savedState = (ViewState["ValidationEnabled"]);

   17                 return (savedState == null) ? false : (bool)savedState;

   18             }

   19             set

   20             {

   21                 ViewState["ValidationEnabled"] = value;

   22             }

   23         }

   24     }

 

Step 2: Implement the Extender Control:

   17  [

   18     ProvideProperty("ValidationEnabled", typeof(Control)), // Names the Property to show on other controls in the designer

   19       Designer(typeof(ExtenderControlDesigner<ValidationProperties>))

   20     ]

   21     public class ValidationProvider : ExtenderControl<ValidationProperties>

   22     {

   23         /// <summary>

   24         /// Defines the controls which can be extended.

   25         /// </summary>

   26         /// <remarks>

   27         /// In general, the Page, the Form, and buttons cannot be extended.

   28         /// </remarks>

   29         public override Boolean CanExtend(Object target)

   30         {

   31             if (target is Control && !(target is ValidationProvider))

   32             {

   33                 return true;

   34             }

   35             return false;

   36         }

   37 

   38         /// <summary>

   39         /// gets the property value for the given control

   40         /// </summary>       

   41         public bool GetValidationEnabled(Control control)

   42         {

   43             return EnsureControlItem(control).ValidationEnabled;           

   44         }

   45 

   46         /// <summary>

   47         /// sets the property value for the given control

   48         /// </summary>

   49         public void SetValidationEnabled(Control control, bool validationEnabled)

   50         {

   51             EnsureControlItem(control).ValidationEnabled = validationEnabled;           

   52             NotifyDesignerOfChange();

   53         }

   54 

   55         private ValidationProperties EnsureControlItem(Control control)

   56         {

   57             if (control == null)

   58             {

   59                 throw new ArgumentNullException("control");

   60             }

   61             ValidationProperties item;

   62 

   63             if (this.TargetProperties.Contains(control.ID))

   64                 item = this.TargetProperties[control.ID];

   65             else

   66             {

   67                 item = new ValidationProperties();

   68                 item.ControlId = control.ID;

   69                 this.TargetProperties.Add(item);

   70             }

   71             return item;

   72         }   

   73     }


This looks a lot like the amount of code needed to make an IExtenderProvider in Windows Forms.


Download the full source here (you have to add a webproject yourself)

I have to add a disclaimer for the use of this source because this control is not being tested thoroughly or even used till now, it can contain serious bugs!
I didn't even added XML documentation and such. (shame on me) .


The source contains a solution with 2 projects:
Naber.Web.UI                   Contains the base classes to use when making an IExtenderProvider
Naber.Web.UI.Controls      An example ASP.NET Provider using the Generic baseclasses

 

Let me know what you think about this.


 

 

 

 

 

Filed under:

Comments

# paullauyc said on Tuesday, October 10, 2006 7:58 PM

Actually, this is a good example to how to write the extender provider in webform (ASP 2.0). But I have a big problem to modify your code. When I change the “ValidationEnabled” Boolean type to Custom data type class which can’t serialize inside the asp form. For example: My custom data class type has a two string field (Expression, ErrorMessage).  

# paullauyc said on Wednesday, October 11, 2006 2:01 AM

I solved the problem yet.  I need to add some codes for below.

ValidationProvider.cs

 private LabelNamingProperties EnsureControlItem(Control control)

       {

           if (control == null)

           {

               throw new ArgumentNullException("control");

           }

           LabelNamingProperties item;

           if (this.TargetProperties.Contains(control.ID))

               item = this.TargetProperties[control.ID];

           else

           {

               item = new LabelNamingProperties();

               item.ControlId = control.ID;

               this.TargetProperties.Add(item);

----------->-->   NotifyDesignerOfChange();

           }

           return item;

       }

ValidationProperties.cs

namespace Naber.Web.UI.Controls

{

[NotifyParentProperty(true)]

       [Bindable(true),

       DesignerSerializationVisibility(DesignerSerializationVisibility.Content),

        PersistenceMode(PersistenceMode.InnerProperty)]

   public class ValidationProperties : TargetControlProperties

   {

       [NotifyParentProperty(true)]

       public CustomType ValidationEnabled

{

           get

           {

………

………

           }

           set

           {

………

………

           }

       }

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 1 and 2 and type the answer here: