1. 程式人生 > >ASP.NET自定義Web伺服器控制元件-Button控制元件

ASP.NET自定義Web伺服器控制元件-Button控制元件

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伺服器button
namespace MyControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:MyButton runat=server></{0}:MyButton>")]
    public class MyButton : 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;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]//生成屬性時,按屬性內部內容生成(例如在此控制元件裡面(Size-Height,Size_Width))
        //[PersistenceMode(PersistenceMode.InnerProperty)]//以子標籤的形式顯示(例如<Size Width="" Height=""/>)
        public Size Size
        {
            get
            {
                if (ViewState["Size"] == null) {
                    ViewState["Size"] = new Size();
                }
                return (Size)ViewState["Size"];
            }

            set
            {
                ViewState["Size"] = value;
            }
        }
        //定義控制元件的標籤形式
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Input;
            }
        }

        //初始化
        protected override void OnInit(EventArgs e)
        {
            this.Style.Add("width", Size.Width + "px");
            this.Style.Add("height", Size.Height + "px");
            this.Attributes.Add("type", "submit"); //提交按鈕
            this.Attributes.Add("value",Text);
            this.Attributes.Add("name",this.UniqueID);//回發事件必須有的一個屬性
            base.OnInit(e);
        }
        //列印當前控制元件的內容
        protected override void RenderContents(HtmlTextWriter output)
        {
            //output.Write(Text);
        }
        
        public delegate void ClickHandle();
        private object key=new object();
        public event ClickHandle Click {
            add {
                this.Events.AddHandler(key,value);
            }
            remove {
                this.Events.RemoveHandler(key, value);
            }
        }
        //按鈕的回發事件
        public void RaisePostBackEvent(string eventArgument)
        {
            ClickHandle handle = (ClickHandle)base.Events[key];
            if (handle != null) {
                handle();
            }
        }
    }
}

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="MyControls" namespace="MyControls" tagprefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <!--自定義伺服器按鈕控制元件-->
        <cc1:MyButton ID="MyButton1" Size-Height="30" Size-Width="290" OnClick="btnSubmit" Text="我是一個單獨的提交按鈕(自定義伺服器)" runat="server" />
    </div>
 
    
    </form>

</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //自定義伺服器控制元件
    protected void btnSubmit() {
        Response.Write("我是自定義伺服器控制元件的點選事件");
    }
}