Bogey
Bogey

Reputation: 11

How to execute a method right after a Page has been rendered?

I'm writing a C# app that uses Page-controls to display some information. Here's what I would like to do...

  1. Load / Navigate to a page
  2. Display the actual page
  3. Then, execute a method immedtiately after the page is loaded and is already being displayed

For step 1, I'm basically using

this.NavigationService.Navigate(new ThePage())

to load the page. The page's constructor as well as my event handling function is

public partial class ThePage : Page
  {
  public ThePage()
  {
    InitializeComponent();
    Loaded += new RoutedEventHandler(Page_Load);
  }
  protected void Page_Load(object sender, EventArgs e)
  {
    MessageBox.Show("hey");
  }
}

So what happens is that the MessageBox is being shown BEFORE that actualy page is displayed. I'd like to have it the other way around tho, I'd like the page to be displayed and THEN show the message box.

I know I could use a timer, but it feels like this would be a crappy solution since I'd have to guess its interval and either risk making it too short, or otherwise artifially increasing loading time by setting its interval too long.

Upvotes: 1

Views: 1406

Answers (2)

gouderadrian
gouderadrian

Reputation: 431

I've had a similar problem in which I was showing a Messagebox on loading the document. But, the Messagebox was showing before all the page content was visible on the page, leaving my page blank, but with the messagebox showing.

I solved this by delaying execution of messagebox as follows:

<Extension()> _
Public Sub MyMessageAlert(ByVal pPage As Page, ByVal pMessageText As String)
  Dim s As String = "setTimeout(function(){window.alert('" & pMessageText & "')}, 10);"
  ScriptManager.RegisterClientScriptBlock(pPage, pPage.GetType, "ClientScript", s, True)
End Sub

This allows the page to load (on a separate thread maybe?), and also shows the messagebox.

Upvotes: 0

Simon Whittemore
Simon Whittemore

Reputation: 166

How about the content rendered event?

public partial class ThePage : Page
{
  public ThePage()
  {
     InitializeComponent();
     ContentRendered+= new RoutedEventHandler(Page_Load);
  }

  protected void Page_Load(object sender, EventArgs e)
  {
     MessageBox.Show("hey");
  }
}

Upvotes: 1

Related Questions