第一步:创建一个ASP.NET WEB应用程序
第二步:在同一个解决方案中创建一个服务控件项目
2.1 再次创建一个asp.net web应用程序。如图:
2.2 然后在这个项目下创建一个Web窗体服务器控件
第三步:编辑为我想要的控件
在这个我这个控件为textbox,所以这个类应该继承这个类
因为我的目的是添加一个自定义的文本框,所以,这个类应该继承TextBox类,因此,将“public class
yinTextControl1:WebControl”改为"public class yinTextControl1:TextBox".
由于TextBox类中包括了Text属性,所以,要讲yinTextControl1中的Text属性重写。讲"public string
Text" 改为"public overridestring Text".
修改后的类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace yincontrol
{[DefaultProperty("Text")][ToolboxData("<{0}:yinTextControl1 runat=server></{0}:yinTextControl1>")]public class yinTextControl1 : TextBox{[Bindable(true)][Category("Appearance")][DefaultValue("")][Localizable(true)]public override string Text{get{String s = (String)ViewState["Text"];return ((s == null) ? String.Empty : s);}set{ViewState["Text"] = value;}}protected override void RenderContents(HtmlTextWriter output){output.Write(Text);}}
}
重新生成看是否报错,无报错则在第一个项目中引入这个项目
然后在第一个项目的网页上打开工具箱就可以看到刚加进去的控件