WebDevGuy
WebDevGuy

Reputation: 353

Calling method in parent from user control

For some business related reasons I made a wizard navigation user control. Pretty simple: Next/Previous/Finish buttons.

Each of the parent pages that use this nav user control have a SaveData function, which saves the data and does a bunch of other stuff.

When the user clicks Next/Previous/Finish I want to call the parent's SaveData function before redirecting to the other page. How should I do this or can you recommend a better way (code examples if possible please)?

Thanks in advance!

Upvotes: 2

Views: 6848

Answers (2)

Emerson Gutierrez
Emerson Gutierrez

Reputation: 11

you only need the delegate:

public delegate void ClickEventHandler(object sender, EventArgs e);
public event ClickEventHandler NextClicked; 

Upvotes: 1

Win
Win

Reputation: 62260

UserControl should not call Parent's function. It is not a good design.

Instead, you want to bubble up the event from UserControl to the Parent page.

Note: my project's namespace is DemoWebForm.

MyUserControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" 
    CodeBehind="MyUserControl.ascx.cs" 
    Inherits="DemoWebForm.MyUserControl" %>

<asp:Button runat="server" ID="NextButton" 
    OnClick="NextButton_Click" Text="Next" />

<asp:Button runat="server" ID="PreviousButton" 
    OnClick="PreviousButtonn_Click" Text="Previous" />

<asp:Button runat="server" ID="FinishButton" 
    OnClick="FinishButton_Click" Text="Finish" />

MyUserControl.ascx.cs

public partial class MyUserControl : System.Web.UI.UserControl
{
    public event ClickEventHandler NextClicked;
    public event ClickEventHandler PreviousClicked;
    public event ClickEventHandler FinishClicked;

    protected void NextButton_Click(object sender, EventArgs e)
    {
        if (NextClicked != null)
        {
            NextClicked(sender, e);
        }
    }

    protected void PreviousButtonn_Click(object sender, EventArgs e)
    {
        if (PreviousClicked != null)
        {
            PreviousClicked(sender, e);
        }
    }

    protected void FinishButton_Click(object sender, EventArgs e)
    {
        if (FinishClicked != null)
        {
            FinishClicked(sender, e);
        }
    }
}

Parent.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Parent.aspx.cs" 
    Inherits="DemoWebForm.Parent" %>

<%@ Register Src="MyUserControl.ascx" TagName="MyUserControl" TagPrefix="uc1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <uc1:MyUserControl ID="MyUserControl1" runat="server"
            OnNextClicked="MyUserControl1_NextClicked"
            OnPreviousClicked="MyUserControl1_PreviousClicked"
            OnFinishClicked="MyUserControl1_FinishClicked" />
        <asp:Literal runat="server" ID="MessageLiteral" />
    </form>
</body>
</html>

Parent.aspx.cs

public partial class Parent : System.Web.UI.Page
{
    protected void MyUserControl1_NextClicked(object sender,EventArgs e)
    {
        MessageLiteral.Text = "Next button was clicked";
    }

    protected void MyUserControl1_PreviousClicked(object sender, EventArgs e)
    {
        MessageLiteral.Text = "Previous button was clicked";
    }

    protected void MyUserControl1_FinishClicked(object sender, EventArgs e)
    {
        MessageLiteral.Text = "Finish button was clicked";
    }
}

Upvotes: 5

Related Questions