Hold a data structure over several pages?
I'm using .NET 3.5 and I need some help with how to hold a data object when you transfer between different pages.
This is my setup:
I have a four step registration where each step includes its own web page. I want to be able to hold a object in memory between开发者_StackOverflow the pages without saving it to the database. Each page will add something to this object.
Just for the ease of it, lets say I have a object like
public class MyObject
{
private int myNumber;
private String myName;
private List<Person> myFriends; //Person is simply a class that has a Strign name with getter and setters.
public MyObject()
{
myFriends = new List<Person>();
}
public void setMyNumber(int i){
myNumber = i;
}
public void setMyName(String n)
{
myName = n;
}
public void setMyFriends(List<Person> li)
{
myFriends = li;
}
public void addFriend(Person p)
{
myFriends.Add(p);
}
}
When I then get to the last page and have collected all the data, then I will commit it to my database. What is the best way to do this in c#? Any nice links or samples would be great!
You can use session/cookie to hold the data.
See the sample code of how to use session below.
How to: Save Values in Session State
string firstName = "Jeff";
string lastName = "Smith";
string city = "Seattle";
Session["FirstName"] = firstName;
Session["LastName"] = lastName;
Session["City"] = city;
How to: Read Values from Session State
string firstName = (string)(Session["First"]);
string lastName = (string)(Session["Last"]);
string city = (string)(Session["City"]);
Reference:
http://msdn.microsoft.com/en-us/library/ms178581.aspx#CodeExamples
Http is stateless protocol, you can use Session variable and store the object of Person into Session["variable"] = [Object]
and access it on the last page.
Store the data in session for example
Session["Test"] = "Hello World"; //store in session
string str = (string)Session["Test"];//retrieving from session
So, once the value is stored in session, it can be retrieved from other pages.
If you're using in-process session state mode, you'd be fine storing such object in session.
Otherwise, you'd need to taylor some custom approach, because SQL and StateServer modes will be serializing and unserializing that object every time you want to retrieve and store it - less optimal -. Use session state, even in this operational modes if your object is as simple as having strings, integers or any other basic type (that wouldn't be a problem in terms of performance).
I guess if it's some wizard you won't need to store large data, so you could be fine with session state.
Perhaps you want to store text, or some other big things, and I believe best solution would be take a look to Windows Workflow Foundation which has a persistent storage API so you can manage a workflow (like your wizard) and save some state after finishing some step, but I repeat, this could be a good overkill. Take this advice if this user registration wizard needs a lot of information.
精彩评论