Thursday, February 24, 2011

Event Bubbling with User Control


//User  Control

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

public partial class WebUserControl1 : System.Web.UI.UserControl
{
    public event EventHandler BubbleClick;


    override protected void OnInit(EventArgs e)
    {
        InitializeComponent();
        base.OnInit(e);
    }

    private void InitializeComponent()
    {
        this.Button1.Click += new EventHandler(Button1_Click);
        this.Load += new System.EventHandler(this.Page_Load);

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("WebUserControl1 :: Page_Load <BR>");

    }


    public void Button1_Click(object sender, EventArgs e)
    {
        OnBubbleClick(e);
    }
    protected void OnBubbleClick(EventArgs e)
    {
        if (BubbleClick != null)
        {
            BubbleClick(this, e);
        }
    }
    #region Web Form Designer generated code
   
  

  
    #endregion
}


//Page Control
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
          Response.Write("PageControl1 :: Page_Load <BR>");
    }

    override protected void OnInit(EventArgs e)   //page OnInit
    {

        try
        {

            InitializeComponent();
            base.OnInit(e);
        }
        catch (Exception ex)
        {


        }
    }
    private void InitializeComponent()
    {

        try
        {

            // this.Load += new System.EventHandler(this.Page_Load);
            Btn.BubbleClick += new EventHandler(Btn_BubbleClick);

        }
        catch (Exception ex)
        {

        }
    }
    public void Btn_BubbleClick(object sender, EventArgs e)  //Click Event
    {

        Response.Write("PAge Btn_BubbleClick :: Page_Load <BR>");

    }
}

No comments:

Post a Comment