How to pass values from one page to another page without using sessions and query string

14 03 2008

Passing values in the form to another page is a frequently used and the most common task that is done in the web applications and intranet applications. There are many ways by which you can achieve this task. Some of the ways by which you can do this task is to use the Querystring and the session variables. The other method that is used is the Server.Transfer method which we will be seeing in this article. In the ASP.Net each form is posted onto itself and for transferring the control to another form you can use the Server.Transfer() method with some tweaks.

One important aspect that you should know is the Server.Transfer() transfers the control to another form on the server side itself. You might have used another method called the Response.Redirect(). This method uses the browser to make a new request to another page. You will be passing parameters to the Server.Tranfer() method. The parameters that are transferred to this method are the name of the web form to which the control is passed and a Boolean value to preserve or not to preserve the current form state in the new form. These two values are passed while using the Server.Transfer() method.

The following code is used to transfer the control to a destination page.

Server.Transfer(“destination.aspx”, True)

You might see that the above code contains the name of the page to which the control is transferred and a Boolean value ‘True’ to indicate to preserve the current form state in the destination page.

There is one more task that is to be done to make the above code to work properly. If you execute the above code alone you might encounter an error. The error that is caused is due to an attribute called the EnableViewStateMac which is set to True already in the page directive. You have to check the Boolean value of this attribute. It has to be False to make the Server.Transfer() method work properly. You have to check this attribute in the destination page to which the control is transferred. Once you set this attribute to False the Server.Transfer() method is executed properly.

Let us say that you want to transfer the control to destination.aspx. Then you have to see that page directive looks as given below:

<%@ Page Language=”vb” EnableViewStateMac=”False”%>

The important attribute to note is the EnableViewStateMac. Once you set this attribute value to False you can easily access the form values of the source form in the destination.aspx. Suppose you have a TextBox control in the source form. The ID of the textbox control in the source form is TextBox1 and you use the Server.Transfer method to transfer the value of the TextBox1 to the destination.aspx. You can access the value of the TextBox1 in destination.aspx as given below:

Response.Write(Request.Form(“TextBox1”))

provided you set the value of the EnableViewStateMac attribute properly. This is a simple example of accessing the values of controls in source form in another web form.

Consider a scenario where you have lots of controls in the source web form and you want to display the values of these controls in the destination form. One of the way is to use the Request.Form() method to retrieve the values and assign them to the control you need. There is another way that is easier to achieve the same task. Just name the IDs of the controls in the destination web form to that of the source web form. That’s it! And the controls are populated in the destination form with the values of the source web form.

The code given below is an example of the above scenario.

Source.aspx


txtName.Text = “Peter”

Server.Transfer(“Destination.aspx”, True)


Destination.aspx

<%@ Page Language=”vb” EnableViewStateMac=”False”%>


‘Name (ID) a textbox control in this page to txtName as in the Source.aspx
‘You will see the value of the txtName control in this page displayed automatically.

This is an easy method to access values of the source web form in another web form. As we have discussed earlier the Server.Transfer method is used to transfer the control in the server side itself without the need for the browser to make another request. Hands on experience on these methods would give you a clear idea on this.


Actions

Information

7 responses

27 03 2008
guest

good one….

2 04 2008
Prashant Pandey

good article keep the sprit up, and keep writing good article like this one…

13 06 2008
Naveen

Great Article Brother, Keep the spirit alive thanks alot

15 09 2008
B Julian

This is great, keep up the good work, thanks for the article, it helped alot

2 11 2010
Sumaira

very good article…keep it up.

1 04 2011
sateesh

Hi the post is really nice but the thing is that Response.Write(Request.Form(“TextBox1”));
is not working it is giving an error “Non-invocable member ‘System.Web.HttpRequest.Form’ cannot be used like a method”. I set enableviewstateMac property to false
Pls clarify this ASAP

14 04 2011
jeeva

Hi sateesh

even i got same error
just change it as below it will work fine
Response.Write(Request.Form[“TextBox1”]);

🙂

Leave a reply to jeeva Cancel reply