Mega Code Archive

 
Categories / ASP.Net / User Control And Master Page
 

User control with event

<%@ Page language="c#" %> <%@ Register TagPrefix="uc1" TagName="Control" Src="Control.ascx" %> <script runat="server">   void MultipleReached(object sender, EventArgs e) {     Message.Text="Congratulations!";   } </script> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >  <html>   <body MS_POSITIONING="FlowLayout">     <form id="Control" method="post" runat="server">       <uc1:Control id="Control1"                     runat="server"                     Multiple="5"                     OnMultipleReached="MultipleReached"></uc1:Control>       <asp:Label ID="Message" Runat="server" EnableViewState="False" />      </form>   </body> </html> File: Control.ascx <%@ Control Language="c#" AutoEventWireup="false" Codebehind="Control.ascx.cs" Inherits="Control.Control" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %> <asp:Label id="OutputLabel" runat="server"></asp:Label> File: Control.ascx.cs namespace Control {   using System;   using System.Data;   using System.Drawing;   using System.Web;   using System.Web.UI.WebControls;   using System.Web.UI.HtmlControls;   public abstract class Control : System.Web.UI.UserControl   {     protected static readonly object EventMultipleReached = new Object();     protected System.Web.UI.WebControls.Label OutputLabel;     private int multiple = 10;     public int Multiple      {       get        {         return multiple;       }       set       {         multiple = value;       }     }     private void Page_Load(object sender, System.EventArgs e)     {       if(Application["count"] == null)         Application["count"] = 0;       Application.Lock();       Application["count"] = (int)Application["count"] + 1;       Application.UnLock();       if((int)Application["count"] % Multiple == 0)         OnMultipleReached(EventArgs.Empty);       OutputLabel.Text = Application["count"].ToString();     }     public event EventHandler MultipleReached      {       add        {         Events.AddHandler(EventMultipleReached, value);       }       remove       {         Events.RemoveHandler(EventMultipleReached, value);       }     }     public virtual void OnMultipleReached(EventArgs e)     {       EventHandler MultipleReachedHandler = (EventHandler)Events[EventMultipleReached];       if (MultipleReachedHandler != null)       {         MultipleReachedHandler(this, e);       }     }     override protected void OnInit(EventArgs e)     {       InitializeComponent();       base.OnInit(e);     }          private void InitializeComponent()     {       this.Load += new System.EventHandler(this.Page_Load);     }   } }