好文档就是一把金锄头!
欢迎来到金锄头文库![会员中心]
电子文档交易市场
安卓APP | ios版本
电子文档交易市场
安卓APP | ios版本

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

8页
  • 卖家[上传人]:hs****ma
  • 文档编号:478071297
  • 上传时间:2023-02-18
  • 文档格式:DOCX
  • 文档大小:13.24KB
  • / 8 举报 版权申诉 马上下载
  • 文本预览
  • 下载提示
  • 常见问题
    • 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" "" html xmlns="" 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("我是自定义服务器控件的点击大事"); } } 盼望本文所述对大家的程序设计有所关心。

      更多信息请查看IT技术专栏 ... 。

      点击阅读更多内容
      关于金锄头网 - 版权申诉 - 免责声明 - 诚邀英才 - 联系我们
      手机版 | 川公网安备 51140202000112号 | 经营许可证(蜀ICP备13022795号)
      ©2008-2016 by Sichuan Goldhoe Inc. All Rights Reserved.