
一、新建一个Web 控件库;
二、在WebCustomControl1.cs文件中编制如下代码:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
二、在WebCustomControl1.cs文件中编制如下代码:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace WebControlLibrary1
{
/// <summary>
/// WebCustomControl1 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
private string text;//存储用户输入的文本内容
public int timesToRepeat;//存储用户输入的属性值
[Bindable(true),Category("Appearance"), DefaultValue("")]
public int TimesToRepeat //添加用户自定义文本输出次数的属性
{
get { return timesToRepeat; } set { timesToRepeat=value; }
}
{
/// <summary>
/// WebCustomControl1 的摘要说明。
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
private string text;//存储用户输入的文本内容
public int timesToRepeat;//存储用户输入的属性值
[Bindable(true),Category("Appearance"), DefaultValue("")]
public int TimesToRepeat //添加用户自定义文本输出次数的属性
{
get { return timesToRepeat; } set { timesToRepeat=value; }
}
public string Text //添加用户自定义文本属性
{
get { return text; } set { text = value; }
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
//protected访问仅限于包含类或从包含类派生的类型。
使用 override 修饰符来修改方法、属性、索引器或事件。
HtmlTextWriter类在 Web 窗体页上写出一系列连续的 HTML 特定字符和文本。
此类提供 ASP.NET 服务器控件在将 HTML 内容呈现给客户端时所使用的格式化功能。
protected override void Render(HtmlTextWriter output)
{
for(int i=1;i<=timesToRepeat;i++)
{
output.Write("<h1>"+Text+"</h1><br>");
}
}
}
}
三、将控件编译生成WebControlLibrary1.dll;
四、新建一个ASP.NET应用程序;
五、将生成的WebControlLibrary1.dll文件添加到工具栏中;
{
get { return text; } set { text = value; }
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
//protected访问仅限于包含类或从包含类派生的类型。
使用 override 修饰符来修改方法、属性、索引器或事件。
HtmlTextWriter类在 Web 窗体页上写出一系列连续的 HTML 特定字符和文本。
此类提供 ASP.NET 服务器控件在将 HTML 内容呈现给客户端时所使用的格式化功能。
protected override void Render(HtmlTextWriter output)
{
for(int i=1;i<=timesToRepeat;i++)
{
output.Write("<h1>"+Text+"</h1><br>");
}
}
}
}
三、将控件编译生成WebControlLibrary1.dll;
四、新建一个ASP.NET应用程序;
五、将生成的WebControlLibrary1.dll文件添加到工具栏中;


六、在工具栏中将添加的用户自定义控件添加到页面;
七、设置控件的Text属性为“Hello”,设置控件的TimesToRepeat属性为5。
七、设置控件的Text属性为“Hello”,设置控件的TimesToRepeat属性为5。


转载于:https://blog.51cto.com/chenxing/58196