My Profile

Sunday, February 6, 2011

Server.Transfer

1. Server.Transfer(<pagename>)

In the previous method, we have used two different objects of ASP.NET and those are RESPONSE and REQUEST objects. These two objects are important ASP.NET objects which provide output to the client using RESPONSE object and takes input from the client using Request object.
Here the output in the sense not only displaying something on client but also giving some instructions to the client. for example, the Redirect method of the Response object gives the instruction to the client browser to navigate to the given page.
      ASP.Net is mainly built with these kind of objects. Not more than 10 important objects are available in entire ASP.NET [excluding some .Net common objects].
     Server is also on ASP.NET Object which represents or refers to the server environment where our application is running.
    Transfer method of this object provides navigation from one page to other page and this page should be located within our website only. This method also has an ability to carry values from one page to other page.

Server Environment:
The environment where our website is currently running is called Server Environment. The following figure depicts what happens at the server side.



Here in the figure the three objects are shown that are as follows:
·       Response: It is used to provide output to the client, an output may be an instruction to the client browser also.
·       Request: It is used to take the Inputs from the client browser.
·       Server: It represents or refers to the server environment where our current project is running. Server is a pointer object which points to the complete server environment.

Example:
               Server.Transfer(“destpage.aspx”); //correct [when the page destpage.aspx was created in our project]
               Server.Transfer(“http://www.google.co.in”); //wrong [because Google home page is not the page belong to our project]
Simply remember that Server object refers to only our website but not cross-website.
Example: You can understand more easily with the following example.
Step 1:  Goto Solution Explorer à Right click and Add New Item à select “Web Form” à name it as “ServerNavig.aspx” 
Step 2: Design the page as following. 


Controls Used:
Payments and Google --> LinkButton
Input for acc.no and name --> TextBox
Submit button --> Button
Step 3:  Create another page with name result.aspx and design as following



Step 4: Coding
·       Under the Payments[LinkButton] click Event [double click on it and write the following code]

                 Server.Transfer(“payments.aspx”);

//here create a page with name payments.aspx

·       Under the Google[LinkButton] click Event [double click on it and write the following code]

                  Server.Transfer(“http://www.google.co.in”); //wrong

·       Under the Submit[Button] click Event [double click on it and write the following code]

                Server.Transfer(“result.aspx”,true);

//here the result.aspx page is which we created above and the second argument is important here, which is a Boolean property which specifies whether to transfer the values of the current page to destination page or not. True indicates to pass the values to the other form. It transfers all the values of the form. We don’t have an option to send only some values.

·       Under Page_Load event of result.aspx page [double click on the form and write the following code init]

                TextBox1.text = Request[“TxtAccNo”];
                TextBox2.text = Request[“TxtName”];

//in the previous method of navigation (Response.Redirect()) we are writing the Query String, so we know the name for the values to read. But here in this method we are not writing any query string just we are mentioning whether to pass values are not using Boolean property. So in order to read values in this case we use the name for values same as the control name [ID] in which the value is present.

//Save all the project Content and Run it too see the result.


Note:
Server.Transfer doesn’t update the browser URL as it navigates to new page at server only. Whereas,
Response.Redirect will update browser URL as it redirects via browser instead of directly at server.


Differences Between Response.Redirect and Server.Transfer
Response.Redirect                                                         Server.Transfer
1.    Can navigate to                            1. Can navigate
Any site page.                                       Within site only.
2.    Query String should be                2. No query string is
Used for sending values.                      Required, bool is   enough.
3.    Query String values                     3. Server.Transfer 
Appear in URL.                                  Values will not appear.


 ---------------------------------------------------------- 
** If you like this post please like our page in facebook

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More