Monday, September 7, 2009

Pass Value to Destination Page When Using Server.Transfer

It will not be possible to pass value as query string to the destination page when using Server.Transfer.

To pass value from source page to destination page when using Server.Transfer we can use the Item collection in HttpContext object. The below code does that,

Source Page
protected void Button1_Click(object sender, EventArgs e)

{

HttpContext CurrentCon = HttpContext.Current;

CurrentCon.Items.Add("CatID", "Computer");

Server.Transfer("Destination.aspx");

}


Destination Page

protected void Page_Load(object sender, EventArgs e)

{

HttpContext CurrentCon = HttpContext.Current;

string CatID = CurrentCon.Items["CatID"].ToString();

Response.Write(CatID);

}

1 comment:

Saurabh Saxena said...

Adea mein job kaise mil gaya; jab Server.Transfer hi nahi pata :(