Passing values from a page to another by means of the Context object

Passing values from a page to another by means of the Context object

Sometimes you may want to gather the user input with a page, and process it in a different page. As you know, ASP.NET web forms can post only to themselves, so you can’t just change the form’s action attribute and make the form post to a different page. And anyway, you may want to do some processing in the first page, and then pass the control to a second page to complete some operations. This second page will need a way to get the user input of the first page of course, so what can you do? The first and simplest solution is to use a direct Response.Redirect call and load the second page with all the required user input values in the querystring. However, this may not be possible if you have to pass a lot of data (the querystring’s length is limited), and you may also want to pass already processed values, and you don’t want the user to see them. A better and more elegant solution is to use the Items collection of the Contect object, that just allow to set and retrieve context-specific variables. In the first page, you set the context values as follows:

Context.Items("FirstName") = txtFirstName.Text

And then transfer the execution to the second page with:

Server.Transfer("WebForm2.aspx")

In the second page you can retrieve the context variables as follows:

Dim firstName as String = CType(Context.Items("FirstName"), String)

This works fine, but if you have to pass many values you can also improve this technique. In the first page’s code-behind class you can add public properties that expose the user input or other values that may result from your processing, and you set them just before transfering to the second page. Here’s an example:

Public Class InputForm : Inherits System.Web.UI.Page    Protected FirstName As System.Web.UI.WebControls.TextBox    Protected LastName As System.Web.UI.WebControls.TextBox    Public FirstNameValue As String = ""    Public LastNameValue As String = ""    Private Sub Finish_Click(ByVal sender As Object, _        ByVal e As System.EventArgs)        FirstNameValue = FirstName.Text        LastNameValue = LastName.Text        Server.Transfer("ProcessData.aspx")    End SubEnd Class

Then, in the second page you get a reference to the first page instance by casting the Context.Handler object to the type of the first page, and then you can read its custom properties. The following lines shows what to do:

Dim input As InputForm = CType(Context.Handler, InputForm)Response.Write("First name: " & input.FirstNameValue)Response.Write("
")Response.Write("Last name: " & input.LastNameValue)

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes