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 o
f
the app, correct? How do I access these variables if in a different page tha
n
the one variable was created in?
Thanks,
JJHere'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 t
han
> the one variable was created in?
> Thanks,
> 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 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 on
e
>page create the varaibles class and then in another page call that varaible
s
>class again to get the values set?
>Thanks,
>JJ
>"Ken Cox [Microsoft MVP]" wrote:
>
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
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:
>
>
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:
>
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/i...te/default.aspx
http://www.aspalliance.com/kenc/passval.aspx
http://www.dotnetjunkies.com/tutori...?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:27E33A9D-D214-4C86-9771-FB0EE8FE9631@.microsoft.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
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:
>
>
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment