访问在一个代码隐藏页面、.net 中的不同代码隐藏页面中实例化的类对象
我正在构建一个具有多个页面的应用程序。第一个 aspx 页面有用户选择选项,主要是单选按钮和下拉菜单。第二页有另一类用户选择选项,依此类推,直到第四页。当用户单击第四个 aspx 页面上的提交按钮时,所有选择都应存储在 sql 表中。
我有一个类(非静态),其属性用于存储所有用户选择。我的问题是如何访问在第 1 页的代码隐藏中实例化的类对象,以及在其他页面的代码隐藏中实例化的类对象,以便我可以将所有选择存储在该对象的属性中,并将这些属性的值存储在第 4 页的 SQL 中。 我无法使用会话,因为会有数百个用户,并且可能会导致我的应用程序崩溃。另外,我想一次将所有选择存储在 SQL 中,以减少多次 SQL 调用。
I am bulding an application that has multiple pages. The first aspx page has user selection options which are mostly radio buttons and dropdown menus. The second page has another category of user selection options and so on till 4th page. At that point of time when user clicks submit button on 4th aspx page all the selections should be stored in a sql table.
I have a class(non-static) with properties to store all the user selections. My question is how can a access the class object which is instantiated in code behind for page1, in code behind for other pages so that I can store all selections in properties of that object and store values of these properies in SQL in 4th page.
I cannot use sessions because there will be 100s of users and could crash my application. Also i want to store all the selections at a time in SQL to reduce multiple SQL calls.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您不想使用会话,因此您可能希望将每个页面的选择存储在 cookie 中。这可以在客户端完成(使用 JavaScript)。
在最后一页上,您实例化您的类并通过不同的 cookie 设置其属性。
Since you don't want to use the Session, you might want to store the selection of each page in a cookie. This could be done client-side (using javascript).
On the last page, you instantiate your class and set its properties from the different cookies.
ASP.NET 支持跨页面回发,它允许您将页面 1 发送回页面 2,并在第二页面的代码后面访问第一页面的控件。您可以在此处找到有关 MSDN 的更多信息: http://msdn.microsoft.com /en-us/library/ms178139.aspx。要让它在四个页面上工作需要一些技巧,而且它不会非常漂亮,但它应该是可能的。
我知道您希望减少 SQL 调用,但就我个人而言,我可能会考虑在数据库中添加某种保存表,并在用户浏览表单的四个页面时将数据添加到该表中。
例如,第一页有一个名为名字的字段,第二页有一个名为姓氏的字段,第三页有一个名为出生日期的字段。我将创建一个包含 ID、名字、姓氏和出生日期列的数据库表。当用户完成第一页时,我将在该表中创建一行并将名字保存到其中。如何管理数据库中的 ID 由您决定,但我会获取 ID 并将其保存在会话状态或使用 ASP.NET 配置文件提供程序的匿名用户配置文件中。
当用户完成第二页时,我会从会话中或我存储它的任何地方获取 ID,并在数据库中的该行上进行更新,保存第二个名称等。当进入最后阶段时,您可以从数据库中提取迄今为止保存的所有内容,并通过您需要的任何业务逻辑运行它。
毕竟,您可以从该保存表中删除数据,因为它现在应该以其最终状态存储在其他地方。如果您确实走这条路,最好经常删除部分完成的数据。可以肯定的是,如果有人在三小时前填写了第一页,他们就不会回来完成剩下的部分。
ASP.NET does support cross page postbacks which allow you to post page 1 back to page 2 and access the controls from the first page in the code behind of the second page. You can find some more information on MSDN here: http://msdn.microsoft.com/en-us/library/ms178139.aspx. It'll take a bit of hacking about to get it to work over four pages and it won't be terribly pretty but it should be possible.
I know you want to reduce SQL calls but personally I'd probably look at adding some sort of holding table into my database and adding data into that table as the user move through the four pages of your form.
For example say page one has a field called first name, page two has a field called last name and page three has a field called date of birth. I'd create a database table with an ID, first name, last name and date of birth column. When the user finishes page one I would create a row in that table and save the first name into it. How you manage the ID's in the database is up to you but I would then grab the ID and hold it in session state or an anonymous user profile using the ASP.NET profile provider.
When the user has finished page two I would grab the ID back from the session, or wherever I've stored it, and do an update on that row in the database saving the second name and so on. When it gets to the final stage you can pull everything saved so far back from the database and run it through any business logic you need to.
After all is said and done you can remove the data from that holding table as it should now be stored elsewhere in its final state. If you do go down this road it's a good idea to remove partially completed data every so often. It's probably a safe bet that if someone filled in page 1 three hours ago they aren't coming back to finish the rest.
为什么不只用第一页创建记录,然后用其他页面更新记录,将记录 ID 存储在会话中?
第 1 页
将记录插入数据库,其中包含第 1 页中的问题,其他为空。检索插入记录的记录 ID 并将其存储在会话中。
Page 2
使用会话中的 id,使用第 2 页中的问题更新数据库中的记录
Page n
Repeat
这还为您提供了允许用户返回并从上次中断的地方继续的选项。作为旁注,我可能会在用户回答任何问题之前创建记录,这样他们就可以安全地使用后退按钮返回第 1 页并更改他们的答案。
Why not just create the record with the first page and update it the record with each other page, storing the record id in session?
Page 1
Insert the record in the database with the questions from page1 and null for the others. Retrieve the record id of the inserted record and store it in session.
Page 2
Update the record in the database with the questions from page2, using the id in session
Page n
repeat
This also gives you the option of allowing the user to return and pickup where they left off. As a side note I'd probably create the record before the user even answers any questions, that way they could safely use the back button to page 1 and change their answers.