Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Passing Variables

Reply
Thread Tools

Passing Variables

 
 
=?Utf-8?B?Sko=?=
Guest
Posts: n/a
 
      10-06-2004

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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?S2VuIENveCBbTWljcm9zb2Z0IE1WUF0=?=
Guest
Posts: n/a
 
      10-06-2004
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sko=?=
Guest
Posts: n/a
 
      10-06-2004
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

 
Reply With Quote
 
Scott Allen
Guest
Posts: n/a
 
      10-06-2004
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" <(E-Mail Removed)>
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


 
Reply With Quote
 
James Thomas
Guest
Posts: n/a
 
      10-06-2004
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


 
Reply With Quote
 
=?Utf-8?B?Sko=?=
Guest
Posts: n/a
 
      10-06-2004
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" <(E-Mail Removed)>
> 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

>
>

 
Reply With Quote
 
Scott Allen
Guest
Posts: n/a
 
      10-06-2004
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" <(E-Mail Removed)>
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" <(E-Mail Removed)>
>> 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

>>
>>


 
Reply With Quote
 
Steve C. Orr [MVP, MCSD]
Guest
Posts: n/a
 
      10-06-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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



 
Reply With Quote
 
=?Utf-8?B?Sko=?=
Guest
Posts: n/a
 
      10-06-2004
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" <(E-Mail Removed)>
> 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" <(E-Mail Removed)>
> >> 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
> >>
> >>

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is difference between Class variables and Instance variables? rahul8143@gmail.com Java 10 06-06-2011 06:43 AM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Session Variables and Static Variables cobus.lombard@gmail.com ASP .Net 1 03-26-2006 11:05 AM
Accessing class member variables - properties or variables? dwok Java 7 03-04-2005 03:54 AM
Re: Class public shared Variables vs. Application Variables in ASP.NET avnrao ASP .Net 0 05-07-2004 05:28 AM



Advertisments