具有内存数据的 GridView:在保存对象之前将其保存在哪里?
我有一个带有 ObjectDataSource 的 GridView 。我想让用户创建几个对象,然后一次性保存它们。
背景:我想为菜谱创建一个编辑器。食谱有基本属性(如名称和来源)并且有成分(如 100 克黄油),我希望用户设置食谱的所有属性,并让他在将其保存到数据库之前(!!)定义其成分。此外,还必须能够从列表中删除成分。首先保存配方然后定义成分不是一种选择。
我的问题: 在将成分保存到数据库之前,我应该将它们保存在哪里?
我发现:
- ViewState 不是一个选项,因为 ObectDataSource 的 GetData 方法是静态的,因此无法访问视图状态。
- 会话不是一个选项,因为用户应该能够同时在同一浏览器的不同选项卡中使用同一页面。
- 上下文不是一个选项,因为它无法在回调中保留下来。
- 使用 LinqDataSource 而不是 ObectDataSource 不起作用,因为如果数据源不是 linq 数据上下文,它不支持删除操作。
- 在 QueryString 中使用唯一 ID(以识别正确的会话对象)将是一种解决方法,但它会弄乱 URL。
我在网上搜索了一段时间,尝试了很多方法但都没有成功。
非常感谢您的任何帮助和建议!
I have a GridView with an ObjectDataSource. I want to let the user create several objects and then save them in one go.
Background: I want to create an editor for a recipe. A recipe has base properties (like name and origin) and it has ingredients (like 100 g butter) I want the user to set all properties of the recipe and let him define its ingredients before (!!) saving it to the database. Also it must be possible to delete an ingredient from the list. Saving the recipe first and then defining the ingredients is not an option.
My Question:
Where do I keep the ingredients before they are saved to the database?
This what I found out:
- ViewState is not an option because the GetData method of the ObectDataSource is static and therefore cannot access the viewstate.
- Session is not an option because users should be able to use the same page in different tabs of the same browser at the same time.
- Context is not an option because it does not survive the callback.
- Using LinqDataSource instead of ObectDataSource does not work because it does not support delete operations if the data source is not a linq data context.
- Using a unique ID in the QueryString (to identify the correct session object) would be a workaround but it messes up the URL.
I have been searching the web for a while and I have tried a lot of things without success.
Thank you very much for any help and any suggestions!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的选择可能是在页面第一次加载时创建一个临时 ID。将其存储在隐藏字段中,以便视图状态知道它。
使用某种自定义类来表示网格的数据。不使用数据源控件,而是使用网格的 DataSource 和 DataMember 属性手动绑定对象。
使用您生成的临时 ID 作为键将此对象存储在缓存中。
在每次回发时,更新缓存中的对象并将其重新绑定到网格。
Viewstate 是通过临时 ID 维护客户端身份的机制。 asp.net 缓存 API 维护服务器上的数据状态。
Probably the best bet is to create a temp ID the first time the page loads. Store that in a hidden field so viewstate knows about it.
Use a custom class of some sort to represent the data for your grid. Instead of using a datasource control, manually bind the object using the DataSource and DataMember properties of the grid.
Store this object in cache using the temp ID you generate as the key.
On each postback, update the object in cache and rebind it to the grid.
Viewstate is the mechanism that maintains the identify on the client via the temp ID. The asp.net Cache API maintains the data state on the server.
由于似乎没有更优雅的方法来解决这个问题,我将以下代码实现到所有 Web 表单继承的基页类中:
有了这个,我们可以在任何 Web 表单上编写:
而
不是 但是,此解决方案不能由静态方法调用。所以我认为这更多的是一种解决方法而不是解决方案。
如果有人知道更好的方法来解决这个问题,我将不胜感激!
As there seems to be no more elegant way to solve this, I implemented the following code into the base page class from which all my web forms inherit:
With this, we can write on any web form:
instead of
However, this solution cannot be called by a static method. So I think it is more a workaround than a solution.
If anybody knows a better way to solve this, I would be thankful for any feedback!