Skip to main content

Event Bubbling in asp.net

UCBubble.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UCBubble.ascx.cs" Inherits="UCBubble" %>
<fieldset>
    <legend>
        <label for="TextBox1">
            Enter Text and Press Tab</label>
        <asp:TextBox ID="TextBox1" runat="server" />
        <label for="ddlColor">
            Select Color</label>
        <asp:DropDownList AutoPostBack="true" ID="ddlColor" runat="server">
            <asp:ListItem Text="Red" Value="Red"></asp:ListItem>
            <asp:ListItem Text="Green" Value="Green"></asp:ListItem>
        </asp:DropDownList>
    </legend>
</fieldset>
UCBubble.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UCBubble : System.Web.UI.UserControl
{
    public string SelectedIndex
    {
        get { return this.ddlColor.SelectedItem.Value; }
        set
        {
            this.ddlColor.SelectedItem.Value = value;
        }


    }
    public string Text
    {
        get { return this.TextBox1.Text; }
        set { this.TextBox1.Text = value; }
    }

    public bool AutoPostBack
    {
        get { return TextBox1.AutoPostBack; }
        set { TextBox1.AutoPostBack = value; }
    }

  

    protected override void OnInit(EventArgs e)
    {
        this.ddlColor.SelectedIndexChanged += new EventHandler(ddlColor_SelectedIndexChanged);
        this.TextBox1.TextChanged += new EventHandler(TextBox1_TextChanged);
        base.OnInit(e);
    }

    void ddlColor_SelectedIndexChanged(object sender, EventArgs e)
    {
        OnBubbleSelectedIndexChange(e);
    }

    void TextBox1_TextChanged(object sender, EventArgs e)
    {
        OnBubbleTextChanged(e);
    }
    public event EventHandler BubbleIndexChanged;
    public event EventHandler BubbleTextChanged;
    public void OnBubbleSelectedIndexChange(EventArgs e)
    {
        if (BubbleIndexChanged != null)
            BubbleIndexChanged(this, e);
    }

    protected void OnBubbleTextChanged(EventArgs e)
    {
        if (BubbleTextChanged != null)
            BubbleTextChanged(this, e);

    }
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Src="UCBubble.ascx" TagName="UCBubble" TagPrefix="uc1" %>
<!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 id="myBody" runat="server">
    <form id="form1" runat="server">
    <div>
    </div>
    <uc1:UCBubble ID="UCBubble1" runat="server" AutoPostBack="true" runat="server" OnBubbleIndexChanged="ChangeBgColor"
        OnBubbleTextChanged="OnTextChanged" />
    </form>
</body>
</html>
Default.aspx.cs
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 OnTextChanged(object sender, EventArgs e)
    {
        Response.Write(this.UCBubble1.Text);
    }


    protected void ChangeBgColor(object sender, EventArgs e)
    {
       myBody.Attributes.Add("bgcolor", UCBubble1.SelectedIndex);
    }
}
Download Code

Comments

  1. It is very useful to my studies, Thanks a lot...

    ReplyDelete
  2. In the Page if you see the syntax , why we are defining the event OnBubbleTextChanged with the event handler OnTextChanged in design time. Can't we do it in runtime in the override of OnInit(). Please suggest as I am facing error while trying to do the same.

    ReplyDelete

Post a Comment

Popular posts from this blog