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;
//web服务器按钮
namespace MyButton //一个类库,MyButton
{
//(重点1)这两个必须要
[ParseChildren(true)]//将子标签转成属性
[PersistChildren(false)]//
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyButtonCotrol runat=server></{0}:MyButtonCotrol>")]
public class MyButtonCotrol : WebControl,IPostBackEventHandler //继承回发接口
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
//(重点2)规定
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[NotifyParentProperty(true)]
[PersistenceMode(PersistenceMode.InnerProperty)]//表示这个属性我们用内部的标签 表示
[TypeConverter(typeof(SizeInfo2Converter))] //SizeInfo2Converter这个类必须新建
//增加一个属性类似这种( <Size Height="30" Width="100" />如同ListView里面的子控件)
public Size Size
{
get
{
if (ViewState["Size"] == null)
{
ViewState["Size"] = new Size(80, 25); //如果等于null的话就初始化一个Size对象
}
ASP.NET自定义Web服务器控件-Button
最新推荐文章于 2019-05-23 17:38:44 发布
本文档详细介绍了如何在ASP.NET中创建一个名为MyButton的自定义Web服务器控件,该控件扩展了WebControl,并实现了IPostBackEventHandler接口。控件包含Text属性和一个自定义Size属性,支持在设计时通过子标签设置宽度和高度。此外,控件还定义了一个Click事件,允许在页面加载时订阅并处理点击事件。
摘要由CSDN通过智能技术生成