Wednesday, August 25, 2004

VJs Tip Of The Day - August 25th 2004

Maintaining State Across ASP.Net Pages - Continued 1

3.) Using HttpContext/RequestParams with Server.Transfer: We discussed adding to session yesterday, similarly you can add to HttpContext as well and this eliminates the cleaning code required in the next page like the way it is required in Session... There are other advantages too...

If you use Server.Transfer then all your control values are automatically transferred to the next page you do not have to add anything to the context explicitly here rather just access the values in the coming page... The code example will show this more clearly...

If you have certain values other than the above mentioned then you have to do certain special operations to explicitly add them in the first page and retrieve them in the second...

CODE SNIPPET

FirstPage Code:
//These are the special values being added to context
//we will retrieve them in the next page
context.Items.Add("specialVariableName", "specialVariableValue");
//We set the second parameter of Server.Transfer to True this will help
//sending certain values automatically
Server.Transfer("NextPage.aspx", True);


NextPage Code:
string strValueOfFirstPageTextBox;
string strSpeciallyAddedValue;
//Use Request.Params to get values of the standard variables
//which get automatically transferred
strValueOfFirstPageTextBox = Request.Params.Get("firstPageTextBox")
//Use HttpContext to get the values specially added
strSpeciallyAddedValue = context.Items("specialVariableName")

No comments: