ASP.NET组件设计Step by Step(1)

2004.8.1

学习创建工程库、在自己的项目中应用

1、 启动VS200X

2、 选择新建解决方案

3、 选择缺省的方案类型,选择 Web控件库。Web控件库不需要web站点,基本上是一个DLL类型库

4、 输入自己的类型库的名称,譬如ASPCTLLib之类的。这个决定了namespace和以后其他工程使用此库的引用库名。

5、 系统自动产生的代码如下:

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ComponentModel;

 

namespace ASPCtlLib

{

     /// <summary>

     /// WebCustomControl1 的摘要说明。

     /// </summary>

     [DefaultProperty("Text"),

          ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]

     public class WebCustomControl1 : System.Web.UI.WebControls.WebControl

     {

          private string text;

          [Bindable(true),

              Category("Appearance"),

              DefaultValue("")]

         public string Text

         {

              get

              {

                   return text;

              }

              set

              {

                   text = value;

              }

         }

 

         /// <summary>

         /// 将此控件呈现给指定的输出参数。

         /// </summary>

         /// <param name="output"> 要写出到的 HTML 编写器 </param>

          protected override void Render(HtmlTextWriter output)

         {

              output.Write(Text);

         }

     }

}

6、 AssemblyInfo.cs的介绍

7、 编译此工程得到ASPCTLLIb.dll

8、 其他项目引用

如何引用自己建立的Web控件库

1、 打开/新建一个asp.net项目

2、 引用ASPCTLLib.dll,将会在引用中出现aspctllib,同时将之前的dll文件复制到当前工程的目录

3、 将自己的Web控件库中控件加入toolbar的选项卡:右击选项卡,自定义工具箱,选择.net框架组件,浏览,找到aspctllib.dll加入,即可看到名为WebCustomControl1的组件存在于工具箱

4、 打开自己的aps.net web 项目,选择一个web窗体,可以将自己的控件加入到其中。缺省的设计时此控件会显示:[WebCustomControl1 ” WebCustomControl1”]

5、 选中窗体中的此控件,打开属性进行编辑,基本上缺省的设计控件仅有一个Text属性属于定制属性,输入特定文字。

6、 编译,浏览察看效果。

原文地址:https://www.cnblogs.com/jasononline/p/775437.html