Passing Variables

Discussion in 'ASP .Net' started by =?Utf-8?B?Sko=?=, Oct 6, 2004.

  1. 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
    =?Utf-8?B?Sko=?=, Oct 6, 2004
    #1
    1. Advertising

  2. 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
    =?Utf-8?B?S2VuIENveCBbTWljcm9zb2Z0IE1WUF0=?=, Oct 6, 2004
    #2
    1. Advertising

  3. 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
    =?Utf-8?B?Sko=?=, Oct 6, 2004
    #3
  4. =?Utf-8?B?Sko=?=

    Scott Allen Guest

    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" <>
    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
    Scott Allen, Oct 6, 2004
    #4
  5. =?Utf-8?B?Sko=?=

    James Thomas Guest

    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
    James Thomas, Oct 6, 2004
    #5
  6. 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" <>
    > 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

    >
    >
    =?Utf-8?B?Sko=?=, Oct 6, 2004
    #6
  7. =?Utf-8?B?Sko=?=

    Scott Allen Guest

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

    >>
    >>
    Scott Allen, Oct 6, 2004
    #7
  8. 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

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://Steve.Orr.net
    Hire top-notch developers at http://www.able-consulting.com


    "JJ" <> wrote in message
    news:...
    >
    > 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
    Steve C. Orr [MVP, MCSD], Oct 6, 2004
    #8
  9. 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" <>
    > 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" <>
    > >> 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
    > >>
    > >>

    >
    >
    =?Utf-8?B?Sko=?=, Oct 6, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov
    Replies:
    25
    Views:
    841
    E. Robert Tisdale
    Mar 23, 2005
  2. Replies:
    9
    Views:
    924
  3. pereges
    Replies:
    7
    Views:
    420
    pereges
    Jun 1, 2008
  4. Ramon F Herrera
    Replies:
    8
    Views:
    463
    Pascal J. Bourguignon
    Sep 13, 2009
  5. deadpickle
    Replies:
    1
    Views:
    946
    Jens Thoms Toerring
    Nov 7, 2010
Loading...

Share This Page