By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,625 Members | 1,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,625 IT Pros & Developers. It's quick & easy.

Passing Variables

P: n/a
JJ

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
Nov 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Here's one way to pass values from one .aspx page to another:

http://authors.aspalliance.com/kenc/passval.aspx
"JJ" wrote:

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

Nov 18 '05 #2

P: n/a
JJ
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

"Ken Cox [Microsoft MVP]" wrote:
Here's one way to pass values from one .aspx page to another:

http://authors.aspalliance.com/kenc/passval.aspx
"JJ" wrote:

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

Nov 18 '05 #3

P: n/a
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

JJ wrote:

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


Nov 18 '05 #4

P: n/a
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,

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

On Wed, 6 Oct 2004 09:51:01 -0700, "JJ" <JJ@discussions.microsoft.com>
wrote:
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

"Ken Cox [Microsoft MVP]" wrote:
Here's one way to pass values from one .aspx page to another:

http://authors.aspalliance.com/kenc/passval.aspx
"JJ" wrote:
>
> 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


Nov 18 '05 #5

P: n/a
JJ
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

"Scott Allen" wrote:
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,

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

On Wed, 6 Oct 2004 09:51:01 -0700, "JJ" <JJ@discussions.microsoft.com>
wrote:
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

"Ken Cox [Microsoft MVP]" wrote:
Here's one way to pass values from one .aspx page to another:

http://authors.aspalliance.com/kenc/passval.aspx
"JJ" wrote:

>
> 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


Nov 18 '05 #6

P: n/a
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/
On Wed, 6 Oct 2004 11:23:03 -0700, "JJ" <JJ@discussions.microsoft.com>
wrote:
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

"Scott Allen" wrote:
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,

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

On Wed, 6 Oct 2004 09:51:01 -0700, "JJ" <JJ@discussions.microsoft.com>
wrote:
>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
>
>"Ken Cox [Microsoft MVP]" wrote:
>
>> Here's one way to pass values from one .aspx page to another:
>>
>> http://authors.aspalliance.com/kenc/passval.aspx
>>
>>
>> "JJ" wrote:
>>
>> >
>> > 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



Nov 18 '05 #7

P: n/a
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/is...e/default.aspx

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

http://www.dotnetjunkies.com/tutoria...tutorialid=600

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

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com
"JJ" <JJ@discussions.microsoft.com> wrote in message
news:27**********************************@microsof t.com...

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

Nov 18 '05 #8

P: n/a
JJ
Thanks Scott !!!

"Scott Allen" wrote:
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/
On Wed, 6 Oct 2004 11:23:03 -0700, "JJ" <JJ@discussions.microsoft.com>
wrote:
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

"Scott Allen" wrote:
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,

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

On Wed, 6 Oct 2004 09:51:01 -0700, "JJ" <JJ@discussions.microsoft.com>
wrote:

>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
>
>"Ken Cox [Microsoft MVP]" wrote:
>
>> Here's one way to pass values from one .aspx page to another:
>>
>> http://authors.aspalliance.com/kenc/passval.aspx
>>
>>
>> "JJ" wrote:
>>
>> >
>> > 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


Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.