Add Action List to Control
Step 1 : Build the control using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomDesignerActionsDemo
{
[DefaultProperty( "Text" )]
[ToolboxData( "<{0}:UserInfo runat=server>" )]
[Designer( typeof( MyDesigner ) )]
public class UserInfo : WebControl
{
#region Fields
private int m_Age;
protected TextBox txbAge;
[CategoryAttribute( "Misc" )]
[DescriptionAttribute( "My Age" )]
public int Age
{
get
{
return m_Age;
}
set
{
m_Age = value;
}
}
private string m_Email;
protected TextBox txbEmail;
[CategoryAttribute( "Misc" )]
[DescriptionAttribute( "My Email" )]
public string Email
{
get
{
return m_Email;
}
set
{
m_Email = value;
}
}
private DateTime m_Date;
protected TextBox txbDate;
[CategoryAttribute( "Misc" )]
[DescriptionAttribute( "My Date" )]
public DateTime Date
{
get
{
return m_Date;
}
set
{
m_Date = value;
}
}
private CityList m_City;
protected TextBox txbCity;
[CategoryAttribute( "General" )]
[DescriptionAttribute( "My City" )]
public CityList City
{
get
{
return m_City;
}
set
{
m_City = value;
}
}
[Bindable( true )]
[Category( "Appearance" )]
[DefaultValue( "" )]
[Localizable( true )]
public string Text
{
get
{
String s = (String)ViewState[ "Text" ];
return ( ( s == null ) ? String.Empty : s );
}
set
{
ViewState[ "Text" ] = value;
}
}
#endregion
bool IsCreateChildControls = false;
protected void SetValueInTextBox()
{
txbAge.Text = Age.ToString();
txbCity.Text = City.ToString();
txbDate.Text = Date.ToString();
txbEmail.Text = Email;
}
protected override void EnsureChildControls()
{
if ( !IsCreateChildControls )
CreateChildControls();
base.EnsureChildControls();
}
protected override void CreateChildControls()
{
txbAge = new TextBox();
txbCity = new TextBox();
txbDate = new TextBox();
txbEmail = new TextBox();
base.CreateChildControls();
IsCreateChildControls = true;
}
protected override void RenderContents( HtmlTextWriter output )
{
EnsureChildControls();
SetValueInTextBox();
output.Write( Text );
output.Write( "
City: " );
txbCity.RenderControl( output );
output.Write( "
Age: " );
txbAge.RenderControl( output );
output.Write( "
Date: " );
txbDate.RenderControl( output );
output.Write( "
Email: " );
txbEmail.RenderControl( output );
}
}
}
Step 2 : Build the designer for the control using System;
using System.Collections.Generic;
using System.Web.UI.Design;
using System.ComponentModel.Design;
namespace CustomDesignerActionsDemo
{
public class MyDesigner : ControlDesigner
{
private DesignerActionListCollection al = null;
public override DesignerActionListCollection ActionLists
{
get
{
if ( al == null )
{
al = new DesignerActionListCollection();
al.AddRange( base.ActionLists );
// Add a custom DesignerActionList
al.Add( new MyDesignerActionList( this ) );
}
return al;
}
}
}
}
Step 3 : Build the action list for the designer using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace CustomDesignerActionsDemo
{
public class MyDesignerActionList : DesignerActionList
{
MyDesigner m_parent;
public MyDesignerActionList( MyDesigner parent )
: base(parent.Component)
{
m_parent = parent;
}
public override DesignerActionItemCollection GetSortedActionItems()
{
// Create list to store designer action items
DesignerActionItemCollection actionItems = new DesignerActionItemCollection();
// Add Appearance category header text
actionItems.Add(new DesignerActionHeaderItem("Misc"));
//// Add Appearance category descriptive label
actionItems.Add(
new DesignerActionTextItem(
"Properties that affect how the User looks." ,
"Misc" ) );
//// Add Email designer action property item
actionItems.Add(
new DesignerActionPropertyItem(
"Email" ,
"Email" ,
GetCategory( this.WebControl , "Email" ) ,
GetDescription( this.WebControl , "Email" ) ) );
actionItems.Add(
new DesignerActionPropertyItem(
"Age" ,
"Age" ,
GetCategory( this.WebControl , "Age" ) ,
GetDescription( this.WebControl , "Age" ) ) );
actionItems.Add(
new DesignerActionPropertyItem(
"Date" ,
"Date" ,
GetCategory( this.WebControl , "Date" ) ,
GetDescription( this.WebControl , "Date" ) ) );
actionItems.Add(
new DesignerActionPropertyItem(
"City" ,
"City" ,
GetCategory( this.WebControl , "City" ) ,
GetDescription( this.WebControl , "City" ) ) );
actionItems.Add(
new DesignerActionMethodItem(
this ,
"ClearValues" ,
"Clear Values" ,
true ) ); // for the Propeties wondows
return actionItems;
}
#region Control Proxy Properties
public string Email
{
get { return WebControl.Email; }
set { SetProperty( "Email" , value ); }
}
public int Age
{
get{return WebControl.Age;}
set{SetProperty( "Age" , value );}
}
public DateTime Date
{
get{return WebControl.Date; }
set{SetProperty( "Date" , value ); }
}
public CityList City
{
get{return WebControl.City;}
set{SetProperty( "City" , value );}
}
public void ClearValues()
{
SetProperty( "Email" , string.Empty );
SetProperty( "Age" , -1 );
SetProperty( "City" , CityList.Tel_Aviv );
SetProperty( "Date" , DateTime.Now );
}
#endregion
private UserInfo WebControl
{
get
{
return (UserInfo)this.Component;
}
}
private string GetCategory( object source , string propertyName )
{
PropertyInfo property = source.GetType().GetProperty( propertyName );
CategoryAttribute attribute = (CategoryAttribute)property.GetCustomAttributes( typeof( CategoryAttribute ) , false )[ 0 ];
if ( attribute == null )
return null;
return attribute.Category;
}
private string GetDescription( object source , string propertyName )
{
PropertyInfo property = source.GetType().GetProperty( propertyName );
DescriptionAttribute attribute = (DescriptionAttribute)property.GetCustomAttributes( typeof( DescriptionAttribute ) , false )[ 0 ];
if ( attribute == null )
return null;
return attribute.Description;
}
// Helper method to safely set a component’s property
private void SetProperty( string propertyName , object value )
{
// Get property
PropertyDescriptor property = TypeDescriptor.GetProperties( this.WebControl )[ propertyName ];
// Set property value
property.SetValue( this.WebControl , value );
}
}
public enum CityList
{
Tel_Aviv, Ramat_Gan
}
}
User info with Action List !!!!
