ASP.NET控件设计时操作列表与模板编辑浅析

开发 后端
ASP.NET控件设计时操作列表与模板编辑向你介绍了ASP.NET控件设计时操作列表与模板编辑的相关内容,希望本文对你有所帮助。

ASP.NET控件设计时操作列表与模板编辑的基本内容:

 

ASP.NET控件设计时操作列表与模板编辑一.智能标记

先看一张图.

智能标记 

GridView右侧的小三角可以很轻松的帮助我们设置常用的属性,如下面的启动分页,启用排序等,通过这样的方式我们可以很快的完成工作。我们称这样的任务菜单为智能标记.

下面来看看ASP.NET控件设计时操作列表与模板编辑如何实现

1.重写ControlDesigner的ActionLists属性

你必须重写这个属性,返回你自定义的智能标记集合(即DesignerActionListCollection),这里假设CustomControlActionList为自定义的智能

  1. public class SampleControlDesigner : ControlDesigner  
  2. {  
  3.     public SampleControlDesigner()  
  4.         : base()  
  5.     {  
  6.     }  
  7.  
  8.     //创建一个自定义操作列表集合  
  9.     public override DesignerActionListCollection ActionLists  
  10.     {  
  11.         get 
  12.         {  
  13.             DesignerActionListCollection actionLists = new DesignerActionListCollection();  
  14.             actionLists.Add(new CustomControlActionList(this));  
  15.  
  16.             return actionLists;  
  17.         }  
  18.     }    

2.CustomControlActionList 自定义项列表

2.1项列表分类

(1)标题面板

(2)属性面板

(3)方法面板

类图如下

类图 

看个效果图,你就明白怎么回事了

效果图 

2.2实现

(1)继承DesignerActionList类,重写GetSortedActionItems方法添加自定义项面板集合,即2.1的三种项面板

  1. public override DesignerActionItemCollection GetSortedActionItems()  
  2. {  
  3.     if (items == null)  
  4.     {  
  5.         items = new DesignerActionItemCollection();  
  6.         // 添加标题面板  
  7.         items.Add(new DesignerActionHeaderItem("快速设置面板测试:"));  
  8.         //添加属性相关面板  
  9.         items.Add(new DesignerActionPropertyItem("Visible",  
  10.                  "是否显示"));  
  11.         items.Add(new DesignerActionPropertyItem("Width",  
  12.                 "设置宽度"));  
  13.         items.Add(new DesignerActionPropertyItem("Height",  
  14.                "设置高度"));  
  15.         // 添加方法相关面板  
  16.  
  17.         items.Add(new DesignerActionMethodItem(this"FormatBlue""定义背景为蓝色"true));  
  18.         items.Add(new DesignerActionMethodItem(this"FormatRed""定义背景为红色"true));  
  19.         items.Add(new DesignerActionMethodItem(this"FormatWhite""定义背景为白色"true));  
  20.           
  21.     }  
  22.     return items;  

(2)属性,方法项面板的实现

如果你设置属性的话,则必须在CustomControlActionList定义属性,方法也相同,代码如下

  1. #region 自定义方法  
  2.  
  3.         public void FormatBlue()  
  4.         {  
  5.             SampleControl ctrl = (SampleControl)_parent.Component;  
  6.             TransactedChangeCallback toCall = new TransactedChangeCallback(DoFormat);  
  7.             ControlDesigner.InvokeTransactedChange(ctrl, toCall, "FormatBlue""FormatBlue");  
  8.         }  
  9.  
  10.         public void FormatRed()  
  11.         {  
  12.             SampleControl ctrl = (SampleControl)_parent.Component;  
  13.             TransactedChangeCallback toCall = new TransactedChangeCallback(DoFormat);  
  14.             ControlDesigner.InvokeTransactedChange(ctrl, toCall, "FormatRed""FormatRed");  
  15.         }  
  16.  
  17.         public void FormatWhite()  
  18.         {  
  19.             SampleControl ctrl = (SampleControl)_parent.Component;  
  20.             //定义委托  
  21.             TransactedChangeCallback toCall = new TransactedChangeCallback(DoFormat);  
  22.             ControlDesigner.InvokeTransactedChange(ctrl, toCall, "FormatWhite""FormatWhite");  
  23.         }  
  24.  
  25.         #endregion 
  1. #region 自定义属性  
  2.  
  3.         public bool Visible  
  4.         {  
  5.             get 
  6.             {  
  7.                 SampleControl ctrl = (SampleControl)_parent.Component;  
  8.                 return ctrl.Visible;  
  9.             }  
  10.             set 
  11.             {  
  12.                     PropertyDescriptor propDesc = TypeDescriptor.GetProperties(_parent.Component)["Visible"];  
  13.                     propDesc.SetValue(_parent.Component, value);  
  14.  
  15.             }  
  16.         }  
  17.  
  18.         public Unit Width  
  19.         {  
  20.             get 
  21.             {  
  22.                 SampleControl ctrl = (SampleControl)_parent.Component;  
  23.                 return ctrl.Width;  
  24.             }  
  25.             set 
  26.             {  
  27.                 PropertyDescriptor propDesc = TypeDescriptor.GetProperties(_parent.Component)["Width"];  
  28.                 propDesc.SetValue(_parent.Component, value);  
  29.             }  
  30.         }  
  31.  
  32.         public Unit Height  
  33.         {  
  34.             get 
  35.             {  
  36.                 SampleControl ctrl = (SampleControl)_parent.Component;  
  37.                 return ctrl.Height;  
  38.             }  
  39.             set 
  40.             {  
  41.                 PropertyDescriptor propDesc = TypeDescriptor.GetProperties(_parent.Component)["Height"];  
  42.                 propDesc.SetValue(_parent.Component, value);  
  43.             }  
  44.         }  
  45.  
  46.         #endregion  
  47.  
  48.         public bool DoFormat(object arg)  
  49.         {  
  50.             SampleControl ctl = (SampleControl)_parent.Component;  
  51.             string fmt = (string)arg;  
  52.  
  53.             PropertyDescriptor backColorProp = TypeDescriptor.GetProperties(ctl)["BackColor"];  
  54.  
  55.             switch (fmt)  
  56.             {  
  57.                 case "FormatBlue":  
  58.                     backColorProp.SetValue(ctl, Color.Blue);  
  59.                     break;  
  60.                 case "FormatRed":  
  61.  
  62.                     backColorProp.SetValue(ctl, Color.Red);  
  63.                     break;  
  64.                 case "FormatWhite":  
  65.                     backColorProp.SetValue(ctl, Color.White);  
  66.                     break;  
  67.             }  
  68.  
  69.             //刷新设计时html标记  
  70.             _parent.UpdateDesignTimeHtml();  
  71.  
  72.             return true;  
  73.         } 

以上步骤完成以后就大功告成了,接着则与相关控件关联起来就可以了,效果图在上面已经看过了.

[DesignerAttribute(typeof(SampleControlDesigner))]

ASP.NET控件设计时操作列表与模板编辑二.模板编辑器

模板编辑器 

上面的模板编辑界面相信大家都很熟悉吧.设置支持怎么少的了模板呢.设置时模板编辑实现比较简单,下面来看下如何实现

这里自定义的模板控件不再列出

1.重写ControlDesigner类的TemplateGroups返回自定义模板组集合即(TemplateGroupCollection)

添加步骤跟表格的添加类似,td add tr然后table add td

模板则是TemplateGroup add TemplateDefinition 然后TemplateGroupCollection add TemplateGroup

代码如下

  1. public override TemplateGroupCollection TemplateGroups  
  2.         {  
  3.             get 
  4.             {  
  5.  
  6.                 if (col == null)  
  7.                 {  
  8.                     col = base.TemplateGroups;  
  9.  
  10.                     TemplateGroup tempGroup;  
  11.                     TemplateDefinition tempDef;  
  12.                     TemplateGroupsSample ctl;  
  13.  
  14.                     ctl = (TemplateGroupsSample)Component;  
  15.  
  16.                     // 创建模板分组一  
  17.                     tempGroup = new TemplateGroup("模板A组");  
  18.  
  19.                     //提供在设置时编辑模板  
  20.                     tempDef = new TemplateDefinition(this"Template A1",  
  21.                         ctl, "Template1"false);  
  22.  
  23.                     tempGroup.AddTemplateDefinition(tempDef);  
  24.  
  25.                    
  26.                     tempDef = new TemplateDefinition(this"Template A2",  
  27.                         ctl, "Template2"false);  
  28.  
  29.                     tempGroup.AddTemplateDefinition(tempDef);  
  30.  
  31.            
  32.                     col.Add(tempGroup);  
  33.  
  34.                     // 创建模板分组二  
  35.                     tempGroup = new TemplateGroup("模板B组");  
  36.                     tempDef = new TemplateDefinition(this"Template B1",  
  37.                         ctl, "Template3"true);  
  38.                     tempGroup.AddTemplateDefinition(tempDef);  
  39.                     tempDef = new TemplateDefinition(this"Template B2",  
  40.                         ctl, "Template4"true);  
  41.                     tempGroup.AddTemplateDefinition(tempDef);  
  42.                     col.Add(tempGroup);  
  43.                 }  
  44.  
  45.                 return col;  
  46.             }  
  47.         } 

这里注意TemplateDefinition构造函数的***一个属性,true则在设计时编辑只能添加服务器控件

2.初始化启用设计时模板编辑

我们还需要在Initialize方法中调用SetViewFlags方法启用设计时模板编辑

  1. public override void Initialize(IComponent component)  
  2. {  
  3.    
  4.     base.Initialize(component);  
  5.    
  6.     SetViewFlags(ViewFlags.TemplateEditing, true);  

3.提供默认矩形标识符,为控件提供说明

如下图,DataList默认情况下给予如下提示

DataList默认情况 

我们可以通过重写GetDesignTimeHtml方法调用CreatePlaceHolderDesignTimeHtml方法创建一个矩形标识符来实现

  1. public override string GetDesignTimeHtml()  
  2. {  
  3.     return CreatePlaceHolderDesignTimeHtml("右击或选择编辑模板面板来编辑模板内容");  

好了,完成了,接着要做的就是与相关模板控件关联起来了

平时大家都太忙了,上面功能有跟没有没多大关系,不过常用控件属性和功能,有设计时支持一定会让使用的更加有效.

ASP.NET控件设计时操作列表与模板编辑的相关内容就向你介绍到这里,希望对你了解ASP.NET控件设计时操作列表与模板编辑有所帮助。

【编辑推荐】

  1. ASP.NET模板控件开发浅析
  2. ASP.NET数据绑定控件开发浅析
  3. ASP.NET控件设计时支持浅析
  4. ASP.NET2.0数据源控件的用法浅析
  5. ASP.NET控件设计时支持之自动格式设置浅析
责任编辑:仲衡 来源: 博客园
相关推荐

2009-08-07 16:32:52

ASP.NET控件设计时支

2009-08-07 15:24:16

ASP.NET模板控件

2009-08-07 17:49:44

控件设计器

2009-08-07 17:09:24

ASP.NET控件设计时支持

2009-08-07 17:59:35

控件设计器

2009-07-27 17:25:53

ASP.NET验证控件

2009-08-04 14:18:49

ASP.NET邮件列表

2009-08-05 16:53:14

ASP.NET组件设计

2009-08-10 13:32:15

ASP.NET TimASP.NET组件设计

2009-07-24 09:57:25

ASP.NET HTM

2009-08-07 15:34:15

ASP.NET数据绑定

2009-08-04 15:20:59

ASP.NET数据验证数据验证控件

2009-08-03 18:29:31

GridView与Da

2009-08-06 18:18:27

ASP.NET控件开发ASP.NET复合控件

2009-07-28 16:21:03

Asp.net AjaAutoComplet

2009-08-03 18:00:00

ASP.NET服务器控

2009-08-06 17:13:56

ASP.NET自定义控

2009-08-06 15:21:45

ASP.NET控件开发RenderConte

2009-12-02 09:07:45

ASP.NET 4.0

2009-08-10 14:16:59

ASP.NET自定义控
点赞
收藏

51CTO技术栈公众号