我的web控件开发经历(2)——普通类的改装

不希望有太多的未知。如果从control继承,觉得有太多的未知。
尝试1:创建一个普通类库,要public:

using System;
namespace test1
{
    public class test1 
    {
        
    }
}

编译成dll,准备放在测试的aspx页面上,结果连工具箱也放不上,报错。

查看control的定义,8个接口,第一个:IComponent

using System;
using System.ComponentModel;

namespace test1
...{
    public class test1:IComponent
    ...{

        IComponent 成员#region IComponent 成员

        public event EventHandler Disposed;

        public ISite Site
        ...{
            get
            ...{
                throw new Exception("The method or operation is not implemented.");
            }
            set
            ...{
                throw new Exception("The method or operation is not implemented.");
            }
        }

        #endregion

        IDisposable 成员#region IDisposable 成员

        public void Dispose()
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }

   
}

往工具箱放不报错,但看不见。是不是“一个都不能少”呢?把8个都实现了再放,还是一样看不见。反射出control的代码(反射工具reflector可到我上传的资源去下http://download.111cn.net/user/whwqs),发现control头部的元数据,把它加上去。工具箱中可以看见了。

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web;
using System.ComponentModel.Design.Serialization;
using System.Security.Permissions;

namespace test1
...{
    [DesignerCategory("Code"), ToolboxItemFilter("System.Web.UI", ToolboxItemFilterType.Require), DesignerSerializer("Microsoft.VisualStudio.Web.WebForms.ControlCodeDomSerializer, Microsoft.VisualStudio.Web, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), DefaultProperty("ID"), Designer("System.Web.UI.Design.ControlDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), ToolboxItem("System.Web.UI.Design.WebControlToolboxItem, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), Themeable(false), Bindable(true), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]

    public class test1 : IComponent, IDisposable, IParserAccessor, IUrlResolutionService, IDataBindingsAccessor, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor
    ...{       

        IComponent 成员#region IComponent 成员

        public event EventHandler Disposed;

        public ISite Site
        ...{
            get
            ...{
                throw new Exception("The method or operation is not implemented.");
            }
            set
            ...{
                throw new Exception("The method or operation is not implemented.");
            }
        }

        #endregion

        IDisposable 成员#region IDisposable 成员

        public void Dispose()
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        IControlDesignerAccessor 成员#region IControlDesignerAccessor 成员

        public System.Collections.IDictionary GetDesignModeState()
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        public void SetDesignModeState(System.Collections.IDictionary data)
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        public void SetOwnerControl(Control owner)
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        public System.Collections.IDictionary UserData
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        #endregion

        IParserAccessor 成员#region IParserAccessor 成员

        public void AddParsedSubObject(object obj)
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        IUrlResolutionService 成员#region IUrlResolutionService 成员

        public string ResolveClientUrl(string relativeUrl)
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        IDataBindingsAccessor 成员#region IDataBindingsAccessor 成员

        public DataBindingCollection DataBindings
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        public bool HasDataBindings
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        #endregion

        IControlBuilderAccessor 成员#region IControlBuilderAccessor 成员

        public ControlBuilder ControlBuilder
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        #endregion

        IExpressionsAccessor 成员#region IExpressionsAccessor 成员

        public ExpressionBindingCollection Expressions
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        public bool HasExpressions
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        #endregion
    }

}

拖到页面,说什么“不具有id的公共属性",再看control定义,把它的ID拷过来,改装一下,变成:

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web;
using System.ComponentModel.Design.Serialization;
using System.Security.Permissions;

namespace test1
...{
    [DesignerCategory("Code"), ToolboxItemFilter("System.Web.UI", ToolboxItemFilterType.Require), DesignerSerializer("Microsoft.VisualStudio.Web.WebForms.ControlCodeDomSerializer, Microsoft.VisualStudio.Web, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), DefaultProperty("ID"), Designer("System.Web.UI.Design.ControlDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), ToolboxItem("System.Web.UI.Design.WebControlToolboxItem, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), Themeable(false), Bindable(true), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]

    public class test1 : IComponent, IDisposable, IParserAccessor, IUrlResolutionService, IDataBindingsAccessor, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor
    ...{

        string _id = "id1";

        [ParenthesizePropertyName(true)]
        [Filterable(false)]
        [Themeable(false)]
        [MergableProperty(false)]
        public string ID
        ...{
            set
            ...{
                _id = value;
            }
            get
            ...{
                return _id;
            }
        }

        IComponent 成员#region IComponent 成员

        public event EventHandler Disposed;

        public ISite Site
        ...{
            get
            ...{
                throw new Exception("The method or operation is not implemented.");
            }
            set
            ...{
                throw new Exception("The method or operation is not implemented.");
            }
        }

        #endregion

        IDisposable 成员#region IDisposable 成员

        public void Dispose()
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        IControlDesignerAccessor 成员#region IControlDesignerAccessor 成员

        public System.Collections.IDictionary GetDesignModeState()
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        public void SetDesignModeState(System.Collections.IDictionary data)
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        public void SetOwnerControl(Control owner)
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        public System.Collections.IDictionary UserData
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        #endregion

        IParserAccessor 成员#region IParserAccessor 成员

        public void AddParsedSubObject(object obj)
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        IUrlResolutionService 成员#region IUrlResolutionService 成员

        public string ResolveClientUrl(string relativeUrl)
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        IDataBindingsAccessor 成员#region IDataBindingsAccessor 成员

        public DataBindingCollection DataBindings
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        public bool HasDataBindings
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        #endregion

        IControlBuilderAccessor 成员#region IControlBuilderAccessor 成员

        public ControlBuilder ControlBuilder
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        #endregion

        IExpressionsAccessor 成员#region IExpressionsAccessor 成员

        public ExpressionBindingCollection Expressions
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        public bool HasExpressions
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        #endregion
    }

}

再拖到页面,报别的错误,不管,在浏览器查看:

再到control的反射中找到AddParsedSubObject的实现,贴过来:

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web;
using System.ComponentModel.Design.Serialization;
using System.Security.Permissions;

namespace test1
...{
    [DesignerCategory("Code"), ToolboxItemFilter("System.Web.UI", ToolboxItemFilterType.Require), DesignerSerializer("Microsoft.VisualStudio.Web.WebForms.ControlCodeDomSerializer, Microsoft.VisualStudio.Web, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), DefaultProperty("ID"), Designer("System.Web.UI.Design.ControlDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), ToolboxItem("System.Web.UI.Design.WebControlToolboxItem, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), Themeable(false), Bindable(true), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]

    public class test1 : IComponent, IDisposable, IParserAccessor, IUrlResolutionService, IDataBindingsAccessor, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor
    ...{

        string _id = "id1";

        [ParenthesizePropertyName(true)]
        [Filterable(false)]
        [Themeable(false)]
        [MergableProperty(false)]
        public string ID
        ...{
            set
            ...{
                _id = value;
            }
            get
            ...{
                return _id;
            }
        }

        IComponent 成员#region IComponent 成员

        public event EventHandler Disposed;

        public ISite Site
        ...{
            get
            ...{
                throw new Exception("The method or operation is not implemented.");
            }
            set
            ...{
                throw new Exception("The method or operation is not implemented.");
            }
        }

        #endregion

        IDisposable 成员#region IDisposable 成员

        public void Dispose()
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        IControlDesignerAccessor 成员#region IControlDesignerAccessor 成员

        public System.Collections.IDictionary GetDesignModeState()
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        public void SetDesignModeState(System.Collections.IDictionary data)
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        public void SetOwnerControl(Control owner)
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        public System.Collections.IDictionary UserData
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        #endregion

        IParserAccessor 成员#region IParserAccessor 成员

        public void AddParsedSubObject(object obj)
        ...{
            Control child = obj as Control;
            if (child != null)
            ...{
                //this.Controls.Add(child);
            }

        }

        #endregion

        IUrlResolutionService 成员#region IUrlResolutionService 成员

        public string ResolveClientUrl(string relativeUrl)
        ...{
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        IDataBindingsAccessor 成员#region IDataBindingsAccessor 成员

        public DataBindingCollection DataBindings
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        public bool HasDataBindings
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        #endregion

        IControlBuilderAccessor 成员#region IControlBuilderAccessor 成员

        public ControlBuilder ControlBuilder
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        #endregion

        IExpressionsAccessor 成员#region IExpressionsAccessor 成员

        public ExpressionBindingCollection Expressions
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        public bool HasExpressions
        ...{
            get ...{ throw new Exception("The method or operation is not implemented."); }
        }

        #endregion
    }    
}

再浏览,不报错了。但在设计器中还是报错,经过分析我觉得是IControlDesignerAccessor的成员void SetOwnerControl(Control owner)查msdn有一句话:

此成员是显式接口成员的实现。它只能在Control实例被强制转换为 IControlDesignerAccessor 接口时使用。控件设计器使用 IControlDesignerAccessor 接口对控件执行设计时操作。

 

看来必须是Control的实例才能在设计器正常显示,我的可是test1实例。也就是说,不从Control继承,仿照它的实现的类的实例在设计器中总不可能正常显示的。除非仿出一个设计器来。但也是有收获的:在设计器中呈现的控件是真正在内存中对应一个实例的。记住这个IControlDesignerAccessor接口吧。我们做的控件如果在设计器中呈现与种不同的设计界面时可能需要好好了解它。

接下来,有一个思路:我不理会设计器,能呈现我的控件到页面吗?答案是不可能。因为要呈现的控件必须加在控件树上。而加载方法比如:Controls.Add的参数必须是Control或它的子孙。而test1是没有办法转换为control的。

结论:在2.0或之前的框架下,要开发web控件,必须继承自Control。

推荐一篇文章:说明继承自Control继承自WebControl的不同。

http://www.cnblogs.com/mapserver/articles/392325.html

时间: 2024-07-28 18:44:03

我的web控件开发经历(2)——普通类的改装的相关文章

详细阐述利用ASP.NET 2.0创建自定义Web控件开发说明

asp.net|web|创建|控件|控件开发 简介 从使用基本的文本编辑器到创作标记页面,Web 开发已经经历了一个漫长的过程.目前,集成开发环境 (IDE) 为开发过程中的几乎每个方面都提供了图形化表示形式.此外,还实现各种说明性编程技术以提高效率并降低出现错误的几率.Visual Studio 2005 和 ASP.NET 2.0 中的控件体系结构遵循了这些编程趋势,并且提供了可靠的.可扩展的环境,该环境设计为使开发人员可以创建能够以说明方式配置的控件. 此外,ASP.NET 中新的自适应呈

探讨ASP.NET 2.0的Web控件改进之概述

asp.net|web|控件 一. 引言 到目前为止,你可能已经了解了大量的ASP.NET 2.0新特征-母版页面,主题,提供者,等等--所有这样内容都相当精彩:但是,你是否了解到有关定制Web控件开发方面的重大变化?这正是我在本文中所想讨论的.如果你已经从事于控件开发,那么,我想本文所描述的ASP.NET 2.0中的新的改进特征会立即应用于你的控件开发中. 首先应该注意的是,你以前使用ASP.NET 1.1(或1.0)开发的所有Web控件在2.0版本下将继续良好运行-微软并没有破坏你的现有代码

探讨ASP.NET 2.0中的Web控件改进技术

asp.net|web|控件 ASP.NET 2.0并没有抛弃1.1版本中的任何现有控件,而是增加了一组新的控件;同时还引入了若干新的控件开发技术.本系列文章将对这些内容展开全面探讨. 一. 引言 到目前为止,你可能已经了解了大量的ASP.NET 2.0新特征-母版页面,主题,提供者,等等--所有这样内容都相当精彩;但是,你是否了解到有关定制Web控件开发方面的重大变化?这正是我在本文中所想讨论的.如果你已经从事于控件开发,那么,我想本文所描述的ASP.NET 2.0中的新的改进特征会立即应用于

自定义web控件属性请教

问题描述 在asp.net中,以Button为例,其中有一个属性为title,在设计的时候,aspx页面中指定了titile的值之后,程序在运行之后,button以html的数据格发送出来,titile和其值也显示在html中.在asp.net开发过程中,开发一个自定义控件,希望有一个属性,具有类似这种title的特点,应该如何实现? 解决方案 解决方案二: 解决方案三:我要的是web控件开发.解决方案四:先定义个如ToolTip属性呈现的时候把控件的title="+ToolTip"一

ASP.NET控件开发 - 概念和HelloWorld控件

今天我们来看一下如何开发自己的ASP.NET控件.要开发ASP.NET控件首先必须知道一些控件开发的常用基类.如下:1)Control  这是所有控件的基类,所有控件都直接我间接继承它. 2)WebControl  用于开发简单控件,它和Control的区别就是:      WebControl不但继承了Control的所有属性,还增加      了布局,可访问性,外观样式等特性.   (我们等下的HellowWorld控件就要继承它) 3)CompositeControl  用于开发组合控件,

一起学Windows Phone7开发(十三.六 Web控件)

是phone7中的浏览器控件,它是基于Internet Explorer7的,可以直接嵌入到应用程序中.这个控件相对于Windows mobile也有了许多的不同.另外这个控件与MediaElement 一样,只是一个基本显示窗口,所有的控制都需要自已来完成.如Forward.Backward.Refresh等. <phone:WebBrowser Grid.Row="1" HorizontalAlignment="Left" Name="webBr

ASP.NET 控件开发系列之图片切换web控件_实用技巧

开发系列之图片切换web控件_实用技巧-">贴出来控件页面的代码. PicList.ascx 复制代码 代码如下: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="PicList.ascx.cs" Inherits="WebParts_PicList" %> <style type="text/css"&

利用 ASP.NET 2.0 创建自定义 Web 控件

asp.net|web|创建|控件 Jayesh Patel.Bryan Acker.Robert McGovernInfusion Development适用于:Microsoft ASP.NET 2.0Microsoft Visual Studio 2005 摘要:ASP.NET 2.0 中新的自适应呈现模型为控件编写人员提供了很多新的选项.本文展示了这些选项如何使创建 ASP.NET 的自定义控件变得比以前更加容易. 本页内容 简介 自适应呈现模型 创建自定义服务器控件 TagKey 使用

利用ASP.NET 2.0创建自定义Web控件

asp.net|web|创建|控件 从使用基本的文本编辑器到创作标记页面,Web 开发已经经历了一个漫长的过程.目前,集成开发环境 (IDE) 为开发过程中的几乎每个方面都提供了图形化表示形式.此外,还实现各种说明性编程技术以提高效率并降低出现错误的几率.Visual Studio 2005 和 ASP.NET 2.0 中的控件体系结构遵循了这些编程趋势,并且提供了可靠的.可扩展的环境,该环境设计为使开发人员可以创建能够以说明方式配置的控件. 此外,ASP.NET 中新的自适应呈现模型减少了编写