• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

asp 搞代码 4年前 (2022-01-03) 28次浏览 已收录 0个评论

这篇文章主要介绍了ASP.NET自定义Web服务器控件之Button控件,详细讲述了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(“”)] 
    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)]//以子标签的形式显示(例如) 
        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(); 
            } 
        } 
    } 
}

代码如下:
 
 
 
 
 
 
 
 
     
 
 
     
   

 
     
         
   

 
  
     
     
 
 

代码如下:
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(“我是自定义服务器控件的点击事件”); 
    } 
}

希望本文所述对大家的asp.net程序设计有所帮助。

以上就是ASP.NET自定义Web服务器控件之Bu来源gaodai#ma#com搞*代#码网tton控件的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:ASP.NET自定义Web服务器控件之Button控件
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址