posted by 네코냥이 2015. 4. 17. 12:05

Passing Values Using HttpContext

At times, we use Server.Transfer to redirect the user from one page to another page. In this case, one cannot use querystring because the Server.Transfer will redirect to the target page using the same request in the server i.e. there will not be any roundtrip to the client before redirecting and hence the context of the request in the target page is same as the request in source page.

 

In this scenario, we can use the HttpContext object to pass values from source page to destination page. The HttpContext object exposes an Item collection which will accept name-value pairs. This Item collection can be used to attach a value in source page and retrieve it back from the destination page.


※ Transfer 사용시 ,  HttpContext 간의 Item Collection 교환이 이뤄지므로,

이곳에 담아 보내면 좋다. 


Usage

Source.aspx

protected void btnRedirect_Click(object sender, EventArgs e)

    {

        HttpContext _context = HttpContext.Current;

        _context.Items.Add("val", "from previouspage");

       Server.Transfer("Destination.aspx");

    }

Destination.aspx

 protected void Page_Load(object sender, EventArgs e)

    {

        HttpContext _context = HttpContext.Current;

        Response.Write(_context.Items["val"]);

     }