
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. Thats 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 ... <%@ Page
Language="vb" EnableViewStateMac="False"%> 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.
_______________________________________________________________________
FREE
Subscription
Subscribe to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ Recommended
Resource |