Bubbling Event from ASP.NET Web User Controls

I have two UserControls - UserControl1.ascx and UserControl2.ascx in PageDefault.aspx:

How can I call the ( GetLabelText() in UserControl1.ascx) method from UserControl2.ascxusing the bubbling event?

This is my sample code. When I click on the button ( UserControl2Button1 in UserControl1.ascx) - I want to call the method GetLabelText() from UserControl2.ascx- using event bubbles.

PageDefault.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageDefault.aspx.cs" Inherits="TelerikAjaxEvents.PageDefault" %>
<%@ Register TagPrefix="uc" TagName="UserControl1" Src="~/UserControl1.ascx" %>
<%@ Register TagPrefix="uc" TagName="UserControl2" Src="~/UserControl2.ascx" %>
<!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>Page Default</title>
</head>
<body>
    <form id="form1" runat="server">
        UserControl1:
        <uc:UserControl1 ID="UserControl1" runat="server" />

        UserControl2:
        <uc:UserControl2 ID="UserControl2" runat="server" />

    </form>
</body>
</html>

UserControl1.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl1.ascx.cs" Inherits="TelerikAjaxEvents.UserControl1" %>
<asp:Label ID="UserControl1Label1" runat="server"></asp:Label>

UserControl1.ascx.cs

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

        }

        public void GetLabelText() 
        {
            UserControl1Label1.Text = "Text is Visible";
        }
    }

UserControl2.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl2.ascx.cs" Inherits="TelerikAjaxEvents.UserControl2" %>
<asp:Button ID="UserControl2Button1" runat="server" Text="Send" 
    onclick="UserControl2Button1_Click" />

UserControl2.ascx.cs

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

        }

        protected void UserControl2Button1_Click(object sender, EventArgs e)
        {
            //Call method from UserControl1 (GetLabelText()) - Show Label text - USING BUBBLE EVENT
        }
    }
+5
source share
3 answers

. , , System.Web.UI.Control. , , , . BubbleEvent ASP.NET . , MSDN .NET 1.1

Bubbling Event

Bubbling

. , , " " : Bubbling - ASP.NET(#). . .

UserControl1 GetLabelText()

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

    }

    public void GetLabelText() 
    {
        UserControl1Label1.Text = "Text is Visible";
    }
}

UserControl2 BubbleClick, .

public partial class UserControl2 : System.Web.UI.UserControl
{
    protected Button UserControl2Button1;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public event EventHandler BubbleClick;

    protected void OnBubbleClick(EventArgs e)
    {
        if(BubbleClick != null)
        {
            BubbleClick(this, e);
        }
    }      
    protected void UserControl2Button1_Click(object sender, EventArgs e)
    {
        // do some stuff
        OnBubbleClick(e);
    }
}

PageDefault UserControl2 BubbleClick UserControl1.GetLabelText()

public partial class PageDefault : WebPage
{
    UserControl1 userControl1;
    UserControl2 userControl2;

    protected void Page_Load(object sender, EventArgs e)
    {
        UserControl2.BubbleClick += RootBubbleClickHandler;
    }

    protected void RootBubbleClickHandler(object sender, EventArgs e)
    {
        if (sender is UserControl2)
        {
            // subscribe UserControl1 to UserControl2 click event and do something
            userControl1.GetLabelText();
        }

        // check for other controls with "BubbleClicks"         
    }
}
+5

Bubbling - ASP.NET WebForms. ( ), " " ( ).

, , . . Control.RaiseBubbleEvent Control.OnBubbleEvent. (, , ..). , , OnBubbleEvent ItemCommandEvent.

( . ), :

class Page {
    override bool OnBubbleEvent(object sender, EventArgs e) {
         var btn = sender as Button;
         if (btn == null) return false;

         // You may want to do further checking that the source is what you want
         // You could use CommandEventArgs to do this
         this.uc1.GetLabelText();
         return true;
    }
}

:

- Button Clicked
- Button RaiseBubbleEvent
- UserControl OnBubbleEvent returns false (default, since you didn't override)
- Page OnBubbleEvent
+3

Can you try declaring UserControl1 as a public property (e.g. UserControl1) on the PageDefault.aspx page, and then in UserControl2, use Parent.Page.UserControl1.GetLabelText()?

0
source

All Articles