SoFunction
Updated on 2025-03-07

Customize the Button control of the web server control

using System; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
 
//Custom web server 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) ? : s); 
            } 
 
            set 
            { 
                ViewState["Text"] = value; 
            } 
        } 
 
[DesignerSerializationVisibility()]//When generating attributes, generate them according to the contents of the attributes (for example, in this control (Size-Height, Size_Width))
//[PersistenceMode()]// Displayed in the form of a sub-label (for example <Size Width="" Height=""/>)
        public Size Size 
        { 
            get 
            { 
                if (ViewState["Size"] == null) { 
                    ViewState["Size"] = new Size(); 
                } 
                return (Size)ViewState["Size"]; 
            } 
 
            set 
            { 
                ViewState["Size"] = value; 
            } 
        } 
//Define the label form of the control
        protected override HtmlTextWriterTag TagKey 
        { 
            get 
            { 
                return ; 
            } 
        } 
 
//Initialization
        protected override void OnInit(EventArgs e) 
        { 
            ("width", + "px"); 
            ("height", + "px"); 
("type", "submit"); //Submit button
            ("value",Text); 
("name",);//A property that must be available for postback events
            (e); 
        } 
//Print the content of the current control
        protected override void RenderContents(HtmlTextWriter output) 
        { 
            //(Text); 
        } 
         
        public delegate void ClickHandle(); 
        private object key=new object(); 
        public event ClickHandle Click { 
            add { 
                (key,value); 
            } 
            remove { 
                (key, value); 
            } 
        } 
//Button postback event
        public void RaisePostBackEvent(string eventArgument) 
        { 
            ClickHandle handle = (ClickHandle)[key]; 
            if (handle != null) { 
                handle(); 
            } 
        } 
    } 
}