Child to Parent Bubble Up
If you want to pass argument from child control to parent, you can use CommandEventHandler.
Parent ASPX
<%@ Register Src="~/DoesStuff.ascx" TagPrefix="uc1" TagName="DoesStuff" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
<uc1:DoesStuff runat="server" ID="DoesStuff"
OnChildButtonClicked="DoesStuff_ChildButtonClicked" />
</form>
</body>
</html>
Parent Code Behind
public partial class Parent : System.Web.UI.Page
{
protected void DoesStuff_ChildButtonClicked(object sender, EventArgs e) { }
}
Child ASCX
<asp:Button ID="BubbleUpButton" runat="server"
Text="Bubble Up to Parent"
OnClick="BubbleUpButton_OnClick" />
Child Code Behind
public partial class DoesStuff : System.Web.UI.UserControl
{
public event EventHandler ChildButtonClicked = delegate { };
protected void BubbleUpButton_OnClick(object sender, EventArgs e)
{
// bubble up the event to parent.
ChildButtonClicked(this, new EventArgs());
}
}
Parent to Child
It is not a good practice in ASP.Net Web Form to calling one event to another event without underlying control.
Instead, you want to create a public method, and call it from Parent. For example,
// Parent
public partial class Parent : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var doesStuff = DoesStuff1 as DoesStuff;
if (doesStuff != null) DoesStuff1.DisplayMessage("Hello from Parent!");
}
}
// Child
public partial class DoesStuff : System.Web.UI.UserControl
{
public void DisplayMessage(string message)
{
ChildLabel.Text = message;
}
}