如何创建动态数组
为用户创建动态数组以将产品添加到购物篮然后将它们存储在会话变量中的最佳方法是什么,我被告知可序列化数组可以工作,但是在网上寻找解决方案时,我遇到了 ArrayList 看起来很完美,但我似乎无法实现它。
我有一个名为 Basket
的单独类,其中:
ArrayList basketItems = new ArrayList();
我需要能够使用选择链接从网格视图中选择产品,或者使用列表视图并使用我自己的按钮来添加 bookID
到数组,然后该数组将存储在会话变量中并发送到购物篮页面,其中 bookID
将再次针对 SQL 表使用以输出书籍等的详细信息。
What would be the best way to create a dynamic array for a user to add products to a basket then store them in a session variable, I have been told serilizable arrays would work however when looking online for a solution I came accross an ArrayList
which seemed perfect but I can't seem to implement it.
I have a separate class called Basket
with:
ArrayList basketItems = new ArrayList();
I need to be able to select a product from a gridview using the select link or alternatively using a listview and using my own button to then add the bookID
to the array, which will then be stored in a session variable and sent to a basket page where the bookID
will be used again against a SQL table to output the details of the book etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ArrayList 是解决此问题的常用 .NET 1.x 解决方案。如果您使用的是 .NET 2.0 或更高版本,请使用简单的通用列表 (
List
)。像这样:
您可以使用 Add 方法添加项目。
您可以通过这种方式将其分配给会话变量:
并且您可以恢复它:
希望这有帮助
The ArrayList is the usual .NET 1.x solution for this problem. If you are using .NET 2.0 or later, use a simple generic list (
List<T>
).Like this:
You can add items with the Add method.
You can assign it to a session variable this way:
And you can recover it:
Hope this helps
如果我们在 2003 年,那么是的,
ArrayList
本来可以正常工作,但现在我建议您使用通用
List
这将是类型安全的,并且您不需要强制转换。所以你可以拥有以下集合:
If we were in 2003 then yes,
ArrayList
could have worked fine but now I would recommend you using a genericList<T>
which will be type safe and you don't need casting.So you could have the following collection: