ASP.NET自定义Web服务器控件-Button

本文档详细介绍了如何在ASP.NET中创建一个名为MyButton的自定义Web服务器控件,该控件扩展了WebControl,并实现了IPostBackEventHandler接口。控件包含Text属性和一个自定义Size属性,支持在设计时通过子标签设置宽度和高度。此外,控件还定义了一个Click事件,允许在页面加载时订阅并处理点击事件。
摘要由CSDN通过智能技术生成
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对象
                }
          
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值