主页 > 知识库 > ASP.NET自定义Web服务器控件之Button控件

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

热门标签:网站排名优化 百度AI接口 电商新玩法 电销业务 科大讯飞语音识别系统 国美全国运营中心 人工智能 客户服务

本文实例讲述了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("我是自定义服务器控件的点击事件"); 
    } 
}

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

您可能感兴趣的文章:
  • ASP.NET服务器端控件RadioButtonList,DropDownList,CheckBoxList的取值、赋值用法
  • asp.net Page.EnableEventValidation 属性验证服务器控件的回发和回调事件出现的错误
  • jquery获取ASP.NET服务器端控件dropdownlist和radiobuttonlist生成客户端HTML标签后的value和text值
  • asp.net 服务器控件的 ID,ClientID,UniqueID 的区别
  • asp.net下使用Request.From获取非服务器控件的值的方法
  • jQuery生成asp.net服务器控件的代码
  • ASP.NET 动态写入服务器端控件
  • asp.net Page.Controls对象(找到所有服务器控件)
  • Asp.Net使用服务器控件Image/ImageButton显示本地图片的方法

标签:拉萨 南平 咸宁 攀枝花 POS机 枣庄 厦门 益阳

巨人网络通讯声明:本文标题《ASP.NET自定义Web服务器控件之Button控件》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266