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:


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:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS