Topic: State - ContextHandler
Share/Save/Bookmark
Context Handler object
 
Use this object to retrieve public members of one Web form's class from a subsequently displayed Web form.
 
When navigating between Web forms using the Transfer or Execute method, you can retrieve read-only properties
from the previous Web form the first time a subsequent Web form is displayed.
 
The following example declares a public property named "Value" that returns the data entered into a textbox
created no the Context1 Web form:
 
public class Context1 : System.Web.UI.Page
{
   // Create a property to return the value of a text box.
   internal string Value
   {
      get
      {
         return txtValue.Text;
      }
   }
  
   private void butNext_Click(object sender, System.EventArgs e)
   {
      // Transfer to the next page
      Server.Transfer("Context2.aspx");
   }
}
 
When the user clicks the Button control, he is transferred to the Context2 Web form, which retrives the preious
Web form's Context.Handler object, coerces that object to the previous Web form's type, and then accesses the Context1
Value property, as shown here:
 
public class Context2 : System.Web.UI.Page
{
   private void Page_Load(object sender, System.EventArgs e)
   {
      if (!IsPostBack)
      {
         // Declare an object using the previous page's type.
         Context1 LastPage;
         // Coerce the Context.Handler object to the page's type.
         LastPage = (Context1)Context.Handler;
         // Display the Value property
         lblValue.Text = LastPage.Value;
      }
   }
}
 
 
You can transfer complex types from one Web form to another in this way, but remember that the Context.Handler object
reflrects only the immediately previous Web form -- on postback events, it will reflect the currently displayed Web form.
Also remember that the Transfer method does not notify the user's browser that the page address has changed. If the user
clicks REFRESH on the browser, the user receives a warning that the page can't be refreshed without resending information.