views:

275

answers:

1

When I have a child .ASCX control that needs to affect something in the parent page I'm not completely sure how I am supposed to handle the event flow in the case where I need to update something in the parent page.

What I've always ended up doing is putting logic into the 'Pre_Render' event handler.

Since this is processed after any child .ascx controls are processed I can be sure to render the ASPX page correctly before it displays. I just dont think this is a good design and I've always cringed when I've had to do it. But now there is stackoverflow so i can finally ask it!

For instance lets say I have a 'login control' ascx control in a page. The containing page displays a text label in the header bar for 'current logged in user'.

Lets say I click the 'login' button, which will then trigger my authentication and log my user in. The problem is that the text label in the parent page has already been rendered as 'No user logged in'. Thats no good!

By putting the logic into 'PreRender' it will be rendered after the user has logged in. I just dont like this because thats not what PreRender is for.

What is the intended best practice here that I'm missing? I know I could put an event handler on the user control, but that seems clumsy too because there'd be too much coupling.

PS. I'm just using this as an example. I'd had this problem numerous other times so please dont reply telling me how to implement login !

+1  A: 

In your ascx.cs:

public delegate void NavigateEventHandler(int PID); // if you want a custom handler
public event NavigateEventHandler onNavigate;

In your page.aspx.cs:

protected void Page_Init(object sender, EventArgs e) {
    eSelector1.onNavigate += new PostSelector.NavigateEventHandler(eSelector1_Navigate); }
public void eSelector1_Navigate(int PID) {
    eSelector1.PopulateComments(eSelector1.m_PID); }
tsilb