Saturday, May 23, 2009

Pass Variables Between ASP.NET Pages

I simple programming procedure we are using Querystring to passs values from one page to another. You can aslo use Request Object to retrieve the values of the form elements. However, if you have a form with runat=server, and you don't want to expose the variables to the user then your choices are limited. Here is a way to pass variables between ASP.NET pages:

1. In your SendingPage.aspx add an item to the context.
Code: Select all
string variableToPass = "Passed Value";
Context.Items.Add("variableToPass", variableToPass);


2. In your SendingPage.aspx call Server.Transfer to transfer to your ReceivingPage.aspx.
Code: Select all
Server.Transfer("ReceivingPage.aspx", True);


3. In your ReceivingPage.aspx retrieve the variable from the context.

Code: Select all
string receivedValue = null;
receivedValue = Context.Items("variableToPass");



enjoy