Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Server.Transfer in asp.net - Part 53

Suggested Videos
Part 50 - ValidationGroups
Part 51 - Different page navigation techniques
Part 52 - Response.Redirect in asp.net

The following are the different page navigation techniques in asp.net
1. Hyperlink control - Discussed in Part 13 and Part 51 of the ASP.NET video series
2. Response.Redirect - Discussed in Part 52 of the ASP.NET video series
3. Server.Transfer 
4. Server.Execute
5. Cross-Page postback
6. Window.Open 

In this video, We will discuss about 
1. Server.Transfer
2. Difference between Server.Transfer and Response.Redirect



Create an asp.net web application and add 2 webforms. Copy and paste the following HTML on WebForm1.aspx
<div style="font-family: Arial">
    <table>
        <tr>
            <td colspan="2">
                <h1>
                    This is WebForm1</h1>
            </td>
        </tr>
        <tr>
            <td>
                <b>Name</b>
            </td>
            <td>
                :<asp:TextBox ID="txtName" runat="server">
                </asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <b>Email</b>
            </td>
            <td>
                :<asp:TextBox ID="txtEmail" runat="server">
                </asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnTransfer" runat="server" 
                    Text="Transfer to WebForm2" Width="250px" 
                    OnClick="btnTransfer_Click"/>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button ID="btnTransferToExternalWebsite" 
                runat="server" Width="250px" 
                OnClick="btnTransferToExternalWebsite_Click" 
                Text="Transfer to External WebSite"/>
            </td>
        </tr>
    </table>
</div>



Code-Behind code for WebForm1.aspx.cs
protected void btnTransfer_Click(object sender, EventArgs e)
{
    //Send the user to webform2 using Server.Transfer
    //Set the boolean parameter preserveForm=true
    //This ensures that the posted form values can be retrieved
    //Since the default value for this parameter is true, the 
    //form values are preserved, even if this parameter is not used.
    Server.Transfer("~/WebForm2.aspx", true);
}
protected void btnTransferToExternalWebsite_Click(object sender, EventArgs e)
{
    //Transfer to websites/pages on a different web server causes
    //runtime error
    Server.Transfer("http://pragimtech.com/home.aspx");
}

WebForm2.aspx code:
<div>
    <table>
        <tr>
            <td>
                <b>Name</b>
            </td>
            <td>
                :<asp:Label ID="lblName" runat="server">
                </asp:Label>
            </td>
        </tr>
        <tr>
            <td>
                <b>Email</b>
            </td>
            <td>
                :<asp:Label ID="lblEmail" runat="server">
                </asp:Label>
            </td>
        </tr>
    </table>
</div>

WebForm2.aspx.cs code
protected void Page_Load(object sender, EventArgs e)
{
    //Get the form values from the previous page
    System.Collections.Specialized.NameValueCollection nameValueCollection =
        Request.Form;

    lblName.Text = nameValueCollection["txtName"];
    lblEmail.Text = nameValueCollection["txtEmail"];

    //Page previousPage = this.Page.PreviousPage;
    //if (previousPage != null)
    //{
    //    TextBox previousPageNameTextBox = (TextBox)previousPage.FindControl("txtName");
    //    lblName.Text = previousPageNameTextBox.Text;

    //    TextBox previousPageEmailTextBox = (TextBox)previousPage.FindControl("txtEmail");
    //    lblEmail.Text = previousPageEmailTextBox.Text;
    //}
}

The following are the differences between Server.Transfer and Response.Redirect
1. Just like hyperlink and Response.Redirect, Server.Transfer is used to navigate to other pages/sites running on the same web server.
2. Server.Transfer cannot be used to navigate to sites/pages on a different web server.
3. Server.Transfer does not change the URL in the address bar
4. Server.Transfer is faster than Response.Redirect as the redirection happens on the server in one Request/Response cycle. Response.Redirect() involves 2 Request/Response cycles.
5. With Server.Transfer the Form Variables from the original request are preserved.
server.transfer vs response.redirect

2 comments:

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.