473,379 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Passing Variables

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
8 2074
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back...
4
by: Jason Us | last post by:
Does anyone have experience with passing variables from an ASP page to a JSP page. The way it currently works in passing the SSN in the URL. This cannot be good. I thought that storing a...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: Jack | last post by:
Hi, I need to pass multple variables in a link in order to go to a asp page with the two varables. The following are the values of the variables using response.write: <%'Response.Write Mypage...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
6
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A...
3
by: Lee | last post by:
Hi All How can I pass options from one webpage into another webpage. When the user clicks on the hyperlink I want them to be go to the next page but I need to pass in a number that the next...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
6
BezerkRogue
by: BezerkRogue | last post by:
This is the most fundamental action I am sure, but I can't seem to make it happen. I am familiar with passing variables in ASP. But that doesn't seem to be the preferred method in .NET. I have...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.