1

I have a HiddenField on my master page, and its value is set in Page_Load event of the master page. In one of my content pages, I need this value in Page_Load event. So I added <%@ MasterType VirtualPath="~/Site.Master"%> to my content page, and created a property in the master page to read the hidden field's value. But, the Page_Load events seem to be fired in reverse order, that is, first the content page, and then the master page, because my property is always an empty string. Here's the code for my property read method and Page_Load event.

Site = public partial class(System.Web.UI.MasterPage)
  protected 
    method Page_Load(sender : object; e : EventArgs);
  private
    method GetCurrentCenter: String;
  public
    property CurrentCenter: String read GetCurrentCenter;
  end;

method Site.Page_Load(sender : object; e : EventArgs);
begin
  hfCurrentCenter.Value := '1';
end;

method Site.GetCurrentCenter: String;
begin
  Result := hfCurrentCenter.Value;
end;

And here's the Page_Load event in my content page:

method ContentPage.Page_Load(sender : object; e : EventArgs);
var
  Center: DropDownList;
begin
  if Master.CurrentCenter <> '-1' then  // --< Master.CurrentCenter is always an empty string
  begin
    Center := DropDownList(cuWizard.CreateUserStep.ContentTemplateContainer.FindControl("Center"));
    Center.Enabled := False;
  end;
end;

Why is CurrentCenter always an empty string?

Ken White
  • 123,280
  • 14
  • 225
  • 444
iMan Biglari
  • 4,674
  • 1
  • 38
  • 83

1 Answers1

1

I think your problem is related to page lifecycle. if you look at the order of firing of init and load events then you might get your answer.

INIT_EVENTS:
INIT_usercontrols
INIT_MasterPage
INIT_Page

LOAD Events:
Load :Page
Load: MasterPage
Load: UserControl
CodeSpread
  • 327
  • 2
  • 5
  • You are quite right. As I mentioned, the events are fired in that order. So, what should I do? Should I abandon my approach and place my hidden field on all my content pages? – iMan Biglari Nov 28 '12 at 14:32
  • 1
    Its an approach but i have not tried yet. assign the value in init event of masterpage so that it will be available to load event of page – CodeSpread Nov 28 '12 at 14:38
  • I deleted my hidden field, and instead used a `Session` parameter in place of it. Everything works fine... Weird. – iMan Biglari Nov 28 '12 at 14:41
  • 1
    if you like my explanation,please visit my posts at codespread.com – CodeSpread Nov 28 '12 at 14:44