Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

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

Total Hit ( 2708)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
Context.Items("FirstName") = txtFirstName.Text

And then transfer the execution to the second page with:

Click here to copy the following block
Server.Transfer("WebForm2.aspx")

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

Click here to copy the following block
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:

Click here to copy the following block
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 Sub
End 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:

Click here to copy the following block
Dim input As InputForm = CType(Context.Handler, InputForm)
Response.Write("First name: " & input.FirstNameValue)
Response.Write("<br/>")
Response.Write("Last name: " & input.LastNameValue)



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.