Use Server.Transfer to pass data from one page to another page Asp.Net C# VB.Net

More often we need to pass or transfer data from one page to another page. There are a lot of ways to do this. Server.Transfer is one of them. But keep in mind that it’s not possible to pass data using query string from source page to destination page while using Server.Transfer. To pass data from source page to destination page while using Server.Transfer, you have to use HttpContext object to store data in between postback. To do that we will use Default.aspx page as a source page and About.aspx page as a destination page.

To learn Difference Between Response.Redirect vs Server.Transfer please Click Here.

Objective/Outcome:
Using Server.Transfer to pass data from one page to another page Asp.Net Csharp VB.Net

To start the Example Add a Link Button in Default.aspx Page:

<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Use Server.Transfer</asp:LinkButton>

Now under Link Button Click Event write the below code:
C# Code:

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        HttpContext CurrContext = HttpContext.Current;
        CurrContext.Items.Add("Name", "Shawpnendu Bikash Maloroy");
        CurrContext.Items.Add("Address", "Dhaka, Bangladesh");
        Server.Transfer("About.aspx");
    }

VB.Net Code:

    Protected Sub LinkButton1_Click(sender As Object, e As System.EventArgs) Handles LinkButton1.Click
        Dim CurrContext As HttpContext = HttpContext.Current
        CurrContext.Items.Add("Name", "Shawpnendu Bikash Maloroy")
        CurrContext.Items.Add("Address", "Dhaka, Bangladesh")
        Server.Transfer("About.aspx")
    End Sub

Now we need to catch Default page Stored Data in About page:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            HttpContext CurrContext = HttpContext.Current;
            Response.Write(CurrContext.Items["Name"].ToString() + "<br/>");
            Response.Write(CurrContext.Items["Address"].ToString());
        }
    }

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim CurrContext As HttpContext = HttpContext.Current
            Response.Write(CurrContext.Items("Name").ToString() & "<br/>")
            Response.Write(CurrContext.Items("Address").ToString())
        End If
    End Sub

Hope now you can transfer data from one page to another page using Server.Transfer Method.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, VB.Net
One comment on “Use Server.Transfer to pass data from one page to another page Asp.Net C# VB.Net
  1. larry says:

    nice!thank you this helps a lot

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*