Event triggered by child user control can't update parent's controls

48 Views Asked by At

I have a user control (ASCX) that is being used by a parent page (ASPX). I'm trying to call a parent method in the child user control, following the method provided in this post:

My User Control:

public partial class CustomPanel: System.Web.UI.UserControl
{
    public delegate void DisplayMessageHandler(object sender, DisplayMessageEventArgs e);
    public event DisplayMessageHandler DisplayMessage;
    
    private void TriggerParent()
    {
         if(Trigger != null)
             Trigger(this, DisplayMessageEventArgs.Failure("My message"));
    }
}

In parent page load, I've subscribed the event:

protected void Page_Load(object sender, EventArgs e)
{
    CustomPanel.DisplayMessage += new CustomPanel.DisplayMessageHandler(displayHeader);
}

private void displayHeader(Object sender, DisplayMessageEventArgs e)
{
    messageHeader.Text = e.Message;
}

Where in the parent aspx file:

<asp:Label runat="server" ID="messageHeader"></asp:Label>

The problem is after displayHeader() is triggered by TriggerParent(), the rendered parent page does not reflect the changes to messageHeader. I will need to wrap the messageHeader under an UpdatePanel to see the changes.

<asp:UpdatePanel ID="updatePanel1" runat="server" UpdateMode="Always">
    <ContentTemplate>
        <asp:Label runat="server" ID="messageHeader"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

What brings the different between this and a normal Postback triggered by the parent itself? (Controls can be updated without an UpdatePanel.) I want to avoid my user control requiring its parent to wrap controls under UpdatePanels.

0

There are 0 best solutions below