How to access form values of one page in another page using Server.Transfer?

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.

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| How do you implement Observer Design Pattern in .NET? | Introduction to C# (C Sharp)|Debugging in ASP.NET| How do you pass data between different Tiers in .NET Architecture? | How is Classic ADO different from ADO.NET? | How is Dataadapter useful in ADO.NET? | How is Datareader different from Dataset in ADO.NET? | How is .NET Application Development different from Traditional Development? | How is HashTable different from ArrayList in .NET? | How is Inheritance achieved in C#? | How is new keyword different from override keyword during inheritance in .NET? | How is String class different from StringBuilder class in .NET? | Illustrate ADO.NET Architecture | Illustrate the importance of Server.Transfer and Response.Redirect in .NET? | Mention the different objects available in Dataset of ADO.NET | Mention the usage of Connection Object in ADO.NET | What are the commonly used methods of Dataadapter in ADO.NET? | What are the different Behavioral Design Patterns that can be used in .NET Architecture? | What are the different Creational Design Patterns that can be used in .NET Architecture? | What are the different Structural Design Patterns that can be used in .NET Architecture? | What are the methods provided by Command Objects in ADO.NET? | What is Internal Access Modifier in C#? |


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2014 - All Rights Reserved.