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