hw 2 access form values of 1 pg in another pg usng server

2
How to access form values of one page in anothe r page using Server .T ransfer?  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 whi le 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 „Trueto 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 pr operly. 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 al ready 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 de stination.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.  

Upload: ria-roy

Post on 03-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Hw 2 Access Form Values of 1 Pg in Another Pg Usng Server

 

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 commontask that is done in the web applications and intranet applications. There are many waysby which you can achieve this task. Some of the ways by which you can do this task is touse 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 formis posted onto itself and for transferring the control to another form you can use theServer.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 theResponse.Redirect(). This method uses the browser to make a new request to anotherpage. You will be passing parameters to the Server.Tranfer() method. The parametersthat are transferred to this method are the name of the web form to which the control ispassed and a Boolean value to preserve or not to preserve the current form state in thenew 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 istransferred and a Boolean value „True‟ to indicate to preserve the current form state inthe 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 iscaused is due to an attribute called the EnableViewStateMac which is set to True alreadyin the page directive. You have to check the Boolean value of this attribute. It has to beFalse 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 thisattribute 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 tosee that page directive looks as given below: 

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

The important attribute to note is the EnableViewStateMac. Once you set this attributevalue to False you can easily access the form values of the source form in thedestination.aspx. Suppose you have a TextBox control in the source form. The ID of thetextbox control in the source form is TextBox1 and you use the Server.Transfer methodto transfer the value of the TextBox1 to the destination.aspx. You can access the valueof 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 simpleexample of accessing the values of controls in source form in another web form.  

Page 2: Hw 2 Access Form Values of 1 Pg in Another Pg Usng Server

 

Consider a scenario where you have lots of controls in the source web form and youwant to display the values of these controls in the destination form. One of the way is touse the Request.Form() method to retrieve the values and assign them to the controlyou need. There is another way that is easier to achieve the same task. Just name theIDs 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 webform. 

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. Aswe have discussed earlier the Server.Transfer method is used to transfer the control inthe server side itself without the need for the browser to make another request. Handson experience on these methods would give you a clear idea on this.