将数据结构保存在多个页面上?

发布于 2024-11-09 14:16:06 字数 856 浏览 0 评论 0原文

我正在使用 .NET 3.5,并且需要一些帮助来了解如何在不同页面之间传输时保存数据对象。

这是我的设置:

我有一个四步注册,其中每个步骤都包含自己的网页。我希望能够在页面之间的内存中保存一个对象,而不将其保存到数据库中。每个页面都会向该对象添加一些内容。

为了方便起见,假设我有一个像

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);
    }

}

当我到达最后一页并收集了所有数据时的对象,然后我会将其提交到我的数据库中。在 C# 中执行此操作的最佳方法是什么?任何好的链接或示例都会很棒!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

贪了杯 2024-11-16 14:16:06

您可以使用session/cookie来保存数据。

下面是如何使用session的示例代码。

如何:在会话状态中保存值

string firstName = "Jeff";
string lastName = "Smith";
string city = "Seattle";
Session["FirstName"] = firstName;
Session["LastName"] = lastName;
Session["City"] = city;

如何:从会话状态读取值

string firstName = (string)(Session["First"]);
string lastName = (string)(Session["Last"]);
string city = (string)(Session["City"]);

参考:
http://msdn.microsoft.com/en-us/library/ms178581 .aspx#CodeExamples

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

阳光的暖冬 2024-11-16 14:16:06

Http是无状态协议,你可以使用Session变量,将Person的对象存储到Session["variable"] = [Object]中,并在最后一页访问它。

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.

趁年轻赶紧闹 2024-11-16 14:16:06

例如,将数据存储在会话中

Session["Test"] = "Hello World"; //store in session
string str = (string)Session["Test"];//retrieving from session

因此,一旦将值存储在会话中,就可以从其他页面检索它。

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.

谁的新欢旧爱 2024-11-16 14:16:06

如果您使用进程内会话状态模式,则可以将此类对象存储在会话中。

否则,您需要定制一些自定义方法,因为每次您想要检索和存储该对象时,SQL 和 StateServer 模式都会对该对象进行序列化和反序列化 - 不太理想 -。即使在这种操作模式下,如果您的对象像字符串、整数或任何其他基本类型一样简单(这在性能方面不会成为问题),请使用会话状态。

我想如果它是某个向导,您将不需要存储大量数据,因此您可以使用会话状态。

也许您想要存储文本或其他一些大的东西,我相信最好的解决方案是查看 Windows Workflow Foundation,它具有持久存储 API,以便您可以管理工作流程(如向导)并在完成后保存一些状态迈出了一步,但我再说一遍,这可能是一个很大的矫枉过正。如果此用户注册向导需要大量信息,请采纳此建议。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文