zoukankan      html  css  js  c++  java
  • 事件接口

    在开发asp.net过程中,Insus.NET较喜欢写UserControl(用户控件),因为它就是一个灵活的对象。可以在网页随意变换与控制。此次Insus.NET想说的问题,可看如下说明,就比如前一篇《观察者模式与用户控件之间的互动 》,其中UserD与UserC两个用户控件可以交互。这两个用户控件都写了event(事件),delegate(委托)。这部分可以重构一下。把他们写成一个interface(接口),也就是写成一个事件接口。此篇另写例子,让我们学会如何在asp.net开发过程中写事件接口与应用,非以前篇作重构与修改。

    ITransmitable
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    /// <summary>
    /// Summary description for ITransmitable
    /// </summary>

    public delegate void TransmitEventHandler(object sender,string value);

    public interface ITransmitable
    {
        //事件接口
        event TransmitEventHandler Transmit;                 
    }

    两个用户控件分别实作这个接口。UserA,

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

    public partial class UserA : System.Web.UI.UserControl, ITransmitable //实作接口
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public event TransmitEventHandler Transmit;

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Transmit != null)
                Transmit(thisthis.TextBox1.Text);
            this.TextBox1.Text = string.Empty;
        }   
    }

    UserB用户控件实作接口,

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

    public partial class UserB : System.Web.UI.UserControl, ITransmitable
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public event TransmitEventHandler Transmit;

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Transmit != null)
                Transmit(thisthis.TextBox1.Text);
            this.TextBox1.Text = string.Empty;
        }   
    }

     例子演示:

    Demo代码:

    http://download.cnblogs.com/insus/ASPDOTNET/EventInterfaceAndUserControlInteractive.rar

  • 相关阅读:
    使用SQLite数据库实现登陆注册
    使用高德地图api(一)获取调试版和发布版SHA1,包名
    三个痛点简析与启蒙
    统计字符数,行数
    签到app需求分析
    三位数的四则运算
    蒸汽朋克与游戏的结合————《机械迷城》
    结对项目——My note(四)
    结对项目——My note(三)
    结对项目——My note(二)
  • 原文地址:https://www.cnblogs.com/insus/p/2284382.html
Copyright © 2011-2022 走看看