Peuge
Peuge

Reputation: 1491

setting value in MasterPage from UserControl

I have the following situation:

A MasterPage MyMaster.Master

A Content Page Content.aspx

A UserControl MyUserControl.ascx

MyUserControl.ascx is being used in Content.aspx and is being added programatically. The content page is using MyMaster.Master

MyMaster.Master has a variable which I can access from Content.aspx as I have the @MasterType directive set. What I am wanting to do is the following:

1) Set a value in MyUserControl.ascx

2) Access value from Content.aspx

3) Set value in MyMaster.Master

Step 2 is implemented in the PageLoad of content.aspx as follows:

Control ucControl= LoadControl("/UserControls/MyUserControl.ascx");
UserControls_MyUserControl myUC = ucControl as UserControls_MyUserControl;
//Do some caching stuff here
if (myUC != null)
    myUC.PreRender += new EventHandler(myUC_PreRender);

The PreRender handler just sets a value in MyMaster.Master to true. In MyMaster.Master I check that value in PageLoad and try display something if it is true. This does not work.

I suspect it has something to do with the Page Lifecycle, but I cannot seem to find which part is wrong.

Any help much appreciated.

Thanks

Upvotes: 2

Views: 861

Answers (1)

Guffa
Guffa

Reputation: 700322

It's simply that the PreRender event happens after the Load event. You have to check the variable in an event that happens after the one where you set the variable.

You can see the order of the events in the page cycle here: ASP.NET Page Life Cycle Overview

Upvotes: 1

Related Questions