Page.Context Property
.NET Framework 3.0
Gets the HttpContext object associated with the page.
Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)
Assembly: System.Web (in system.web.dll)
The following code example uses the Context property to access the System.Web.HttpContext.AddError(System.Exception) and System.Web.HttpContext.ClearError methods and the System.Web.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.
void Page_Load(Object sender,EventArgs e) { Response.Write("<h3>Page.Context Example:</h3>"); // Add three custom exceptions. Context.AddError(new Exception( "<h3 style='color: red'>New Exception #1.</h3>")); Context.AddError(new Exception( "<h3 style='color: red'>New Exception #2.</h3>")); Context.AddError(new Exception( "<h3 style='color: red'>New Exception #3.</h3>")); // Capture all the new Exceptions in an array. Exception[] errs = Context.AllErrors; foreach (Exception ex in errs) { Response.Write("<p style='text-align:center; "); Response.Write("font-weight:bold'>"); Response.Write(Server.HtmlEncode(ex.ToString()) + "</p>"); } // Clear the exceptions so ASP.NET won't handle them. Context.ClearError(); }
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
("<h3 style=\"color: red\">New Exception #1.</h3>"));
get_Context().AddError(new Exception
("<h3 style=\"color: red\">New Exception #2.</h3>"));
get_Context().AddError(new Exception
("<h3 style=\"color: red\">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("<p style='text-align: center; "
+ "font-weight: bold'>"
+ get_Server().HtmlEncode(ex.ToString())
+ "</p>");
}
// Clear the exceptions so ASP.NET won't handle them.
get_Context().ClearError();
}//Page_Load
Community Additions
ADD
Show: