1、新建一个类库项目
2、添加一个:web自定义控件 (新建后程序会自动有一个类似Label控件设置字符串的类)。
注意:这里要修改一下属性的默认值。自动生成的默认值为 String.Empty 修改为:"我的第一个自定义控件"。然后加入一下注释。
代码如下:
3、生成类库Shift + F6 (我直接生成)
注:到这里一个简单的自定义控件就完成了。
4、新建一个测试网站项目
5、鼠标右击工具箱》选择项(I)... 》浏览...(找到自定义控件的BIN目录Debug下的DLL文件)》添加完成。
a、
b、
c、
这样工具箱中就能看到我自己的控件了。
拖出来看看到底帅不帅:
a、
b、
Code
<cc1:WebCustomControl1 ID="WebCustomControl1_1" runat="server" />
c、
简直就是Label的孪生兄弟。可以修改控件的Text属性,运行一下。真不错...(自己去看吧)
OK完成了。
那么下一文将在该控件基础上详详细细的把细节开发及用到的关键字讲解一下。
(后续......)
I was a newcomer .希望大家多多指点.....
Code
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 ClassLibrary1
{
//[AspNetHostingPermission(System.Security.Permissions.SecurityAction.Assert)]//授权访问
[DefaultProperty("Text")]//默认属性,当选择控件属性是光标定位于第一个编辑的属性
[ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]//控件拖放时默认生成的HTML标记
public class WebCustomControl1 : WebControl
{
[Bindable(true)]//指定将属性绑定到数据对可视化设计器是否有意义
[Browsable(true)]//在属性窗口中是否可见
[Category("Appearance")]//属性要显示位置的分类,如:行为、外观、杂项等等可以在属性窗口看见这样的分类
[DefaultValue("我的第一个自定义控件")]//默认值,与从属性访问器(getter)返回的默认值相同
[Description("我的Label")]//描述是显示在属性窗口底下的
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? "我的第一个自定义控件" : s);
}
set
{
ViewState["Text"] = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(Text);
}
}
}