Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Global.asax access to the current requested Page object

Is there any way i can access the page object from within the global.asax Application_EndRequest function ?

I'm trying to set the text of a label at the end of the request but accessing the page is proving to be more difficult than I thought.

here is what i have that's currently NOT working:

protected void Application_BeginRequest(Object sender, EventArgs e)

    {

        Context.Items.Add("Request_Start_Time", DateTime.Now);

    }

    protected void Application_EndRequest(Object sender, EventArgs e)
    {

        TimeSpan tsDuration = DateTime.Now.Subtract((DateTime)Context.Items["Request_Start_Time"]);

        System.Web.UI.Page page = System.Web.HttpContext.Current.Handler as System.Web.UI.Page;
        if (page != null)
        {
            Label label = page.FindControl("lblProcessingTime") as Label;
            if (label != null)
            {
                label.Text = String.Format("Request Processing Time: {0}", tsDuration.ToString());
            }
        }
    }

page is always null here.

Thanks in advance.

like image 220
John Boker Avatar asked Jan 26 '09 17:01

John Boker


1 Answers

It's probably best just to create a BasePage class from which all your pages should inherit. Then you can put the code within the Unload event of the page and there will be no issue.

like image 113
Noldorin Avatar answered Sep 29 '22 22:09

Noldorin