Passing Variables

G

Guest

Hi,

What's the preferred way to pass variables around to different pages now?
Or if my reading servers me right they are retained in memory for the life of
the app, correct? How do I access these variables if in a different page than
the one variable was created in?

Thanks,

JJ
 
G

Guest

Ken,

Is it possible to create a separate class to house my variables and in one
page create the varaibles class and then in another page call that varaibles
class again to get the values set?

Thanks,

JJ
 
S

Scott Allen

HI JJ:

Yes, one way to do this would be to create an instance of the class
and set all the properties, then add a reference to the class to
HttpContext.Current.Items. The Items collection is around for the
duration of the request. When you get to the next page with
Server.Transfer you can pull the reference out of the Items collection
and party on the values.

HTH,
 
J

James Thomas

I'm not sure of the preferred way but what I usually use to accomplish
this is session state variables that are only "alive" for as long as I
need them. An example:

// Page one
private void transfer(string state, string varname)
{
Session.add(state, varname);
Response.redirect("pagetwo.aspx");
}

// Page two
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack) {
TextBox1.Text = (string)Session[<whatever state variable was above>];
}
}

Not exactly functional but it gets the point across anyway. If there's
a better way, I'd love to know!

James
 
G

Guest

Hi Scott,

To add a reference to a class to HttpCOntext.Current.Items. Can you show me
in C# how to do this?

Thanks,
JJ
 
S

Scott Allen

Sure!

Let's say you are inside a web form code behind file, and you already
have a class defined, like for a Person:

public class Person
{
public Person(string name)
{
Name = name;
}

public string Name = String.Empty;
}

In the Page_Load even handler we can create an instance of person and
stick the reference into the Items collection, than transfer to a
different aspx page:

private void Page_Load(object sender, System.EventArgs e)
{

Person p = new Person("Milo Finkledoodle");
Context.Items["Person"] = p;

Server.Transfer("destination.aspx");

}


Inside of detination.aspx, we can pull this reference out and retrieve
the person's name:

private void Page_Load(object sender, System.EventArgs e)
{
p = Context.Items["Person"] as Person;
Response.Write(p.Name);
}

Inside a page you can reach the current context with the Context
property. If you are not writing code in a page class you can still
access the current context like so:

Person p = HttpContext.Current.Items["Person"] as Person;

Just make sure to add "using System.Web;" at the top of the cs file.

HTH,
 
S

Steve C. Orr [MVP, MCSD]

Here's a nice, simple way to pass values from one page to another:
(VB.NET code)

'Add data to the context object before transferring
Context.Items("myParameter") = x
Server.Transfer("WebForm2.aspx")

Then, in WebForm2.aspx:

'Grab data from the context property
Dim x as Integer = CType(Context.Items("myParameter"),Integer)

Of course there are a number of ways to pass values from one page to
another, such as using the querystring, cookies, session,
context, saving to a temporary table in the database between each page, etc.
You'll have to decide which technique is best for your application.
Here are several good articles on the subject to help you decide.
http://msdn.microsoft.com/msdnmag/issues/03/04/ASPNETUserState/default.aspx

http://www.aspalliance.com/kenc/passval.aspx

http://www.dotnetjunkies.com/tutorials.aspx?tutorialid=600

http://www.dotnetbips.com/displayarticle.aspx?id=79
 
G

Guest

Thanks Scott !!!

Scott Allen said:
Sure!

Let's say you are inside a web form code behind file, and you already
have a class defined, like for a Person:

public class Person
{
public Person(string name)
{
Name = name;
}

public string Name = String.Empty;
}

In the Page_Load even handler we can create an instance of person and
stick the reference into the Items collection, than transfer to a
different aspx page:

private void Page_Load(object sender, System.EventArgs e)
{

Person p = new Person("Milo Finkledoodle");
Context.Items["Person"] = p;

Server.Transfer("destination.aspx");

}


Inside of detination.aspx, we can pull this reference out and retrieve
the person's name:

private void Page_Load(object sender, System.EventArgs e)
{
p = Context.Items["Person"] as Person;
Response.Write(p.Name);
}

Inside a page you can reach the current context with the Context
property. If you are not writing code in a page class you can still
access the current context like so:

Person p = HttpContext.Current.Items["Person"] as Person;

Just make sure to add "using System.Web;" at the top of the cs file.

HTH,

--
Scott
http://www.OdeToCode.com/blogs/scott/


Hi Scott,

To add a reference to a class to HttpCOntext.Current.Items. Can you show me
in C# how to do this?

Thanks,
JJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top