.NET Framework Class Library
Page.Context Property

Gets the HttpContext object associated with the page.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)

Syntax

Visual Basic (Declaration)
Protected Friend Overrides ReadOnly Property Context As HttpContext
Visual Basic (Usage)
Dim value As HttpContext

value = Me.Context
C#
protected internal override HttpContext Context { get; }
C++
protected public:
virtual property HttpContext^ Context {
    HttpContext^ get () override;
}
J#
/** @property */
protected HttpContext get_Context ()
JScript
protected internal override function get Context () : HttpContext

Property Value

An HttpContext object that contains information associated with the current page.
Remarks

This property provides programmatic access to the context the page runs in, including information about the request, response, session, and application.

Example

The following code example uses the Context property to access the HttpContext.AddError and HttpContext.ClearError methods and the HttpContext.AllErrors property. The example creates three custom exceptions using the AddError method and uses the AllErrors property to load these exceptions to an array. It then writes the array to the containing page and uses the ClearError method to clear all the errors from the Context property.

Visual Basic
Sub Page_Load(Sender As Object, e As EventArgs ) 

   Response.Write("<h3>Page.Context Example:</h3>")
      
   ' Add three custom exceptions.
   Context.AddError(new Exception("<font color='red'><h3>New Exception #1.</h3>"))
   Context.AddError(new Exception("<font color='red'><h3>New Exception #2.</h3>"))
   Context.AddError(new Exception("<font color='red'><h3>New Exception #3.</h3>"))
 
   ' Capture all the new Exceptions in an array.
   Dim errs() As Exception = Context.AllErrors
   Dim ex As Exception
   
   For Each ex In errs
         Response.Write("<center><b>" + Server.HtmlEncode(ex.ToString()) + "</b></center><br>")
   Next
 
   ' Clear the exceptions so ASP.NET won't handle them.
   Context.ClearError()
End Sub
C#
void Page_Load(Object sender,EventArgs e) 
{
   Response.Write("<h3>Page.Context Example:</h3>");

   // Add three custom exceptions.
   Context.AddError(new Exception("<font color='red'><h3>New Exception #1.</h3>"));
   Context.AddError(new Exception("<font color='red'><h3>New Exception #2.</h3>"));
   Context.AddError(new Exception("<font color='red'><h3>New Exception #3.</h3>"));
 
   // Capture all the new Exceptions in an array.
   Exception[] errs = Context.AllErrors;
 
   foreach (Exception ex in errs)
   {
      Response.Write("<center><b>" + Server.HtmlEncode(ex.ToString()) + "</b></center><br>"); 
   }
 
   // Clear the exceptions so ASP.NET won't handle them.
   Context.ClearError();
}
J#
void Page_Load(Object sender,System.EventArgs e) 
{
    get_Response().Write("<h3>Page.Context Example:</h3>");

    // Add three custom exceptions.
    get_Context().AddError(new Exception
        ("<font color='red'><h3>New Exception #1.</h3>"));
    get_Context().AddError(new Exception
        ("<font color='red'><h3>New Exception #2.</h3>"));
    get_Context().AddError(new Exception
        ("<font color='red'><h3>New Exception #3.</h3>"));
 
    // Capture all the new Exceptions in an array.
    System.Exception errs[] = get_Context().get_AllErrors();
 
    for (int iCtr=0;iCtr<errs.length;iCtr++) {
        System.Exception ex = errs[iCtr];
        get_Response().Write("<center><b>" 
            + get_Server().HtmlEncode(ex.ToString())
            +"</b></center><br>");
    }
    // Clear the exceptions so ASP.NET won't handle them.
    get_Context().ClearError();
}//Page_Load
Platforms

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information

.NET Framework

Supported in: 2.0, 1.1, 1.0
See Also

Tags :


Page view tracker