ASP.Net 中动态条目表行的干净解决方案
我正在 ASP.Net 中开发一个项目,该项目使用 UpdatePanel 来处理所有 AJAX 请求。我更熟悉使用 jQuery AJAX 方法(和策略),因此我有点难以为我需要的功能找到一个干净的解决方案。
基本上,我需要一个允许我使用 AJAX 动态添加任意多行的表。这些行将包含文本框,其值必须由 ASP.Net 保留,直到我准备好提交表单为止。让事情变得更加复杂的是,数据库中可能已经存在一些记录,这些记录也应该显示预填充的值,并且还必须保留对这些字段的任何更改。
我一开始使用 GridView 并将其数据源设置为数据库记录列表,但不幸的是我相信这意味着我无法向 GridView 添加新行。
任何帮助将不胜感激。请记住,该解决方案必须使用 ASP.Net 控件。
I'm working on a project in ASP.Net that uses the UpdatePanel to handle all AJAX requests. I'm much more familiar with using jQuery AJAX methods (and strategies) so I'm kind of stumped on finding a clean solution for the functionality I need.
Basically I need a table that allows me to dynamically add as many rows as I want using AJAX. These rows will contain text boxes whose values must be preserved by ASP.Net until I'm ready to submit the form. To further complicate matters there may already be records that exist in the database that should display as well with the values pre-populated and any changes to these fields must also be preserved.
I had started out by using a GridView and setting it's datasource to a list of the database records but unfortunately I believe this means I can't add new rows to the GridView.
Any assistance would be much appreciated. Keep in mind the solution must use ASP.Net controls.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信我找到了一个看起来非常简单的解决方案。
当页面加载时,我从数据库中收集所有相关记录,并结合投影选择执行查询表达式以创建匿名类型数组。当单击“添加新行”按钮时,我不会尝试直接添加行或从数据库中检索可能旧的数据,而是对数据网格项本身执行查询表达式并合并一个新的空白项。从那里开始,只需设置数据网格数据源、触发数据绑定并更新更新面板即可。
现在唯一的问题是如何识别行,特别是还没有数据库键的新行。我可以设计一些巧妙的方法来生成行 id,但我感觉 ASP.Net 已经做到了这一点,我需要做的就是将这些 id 与匿名类型相关联。
I believe I figured out a solution that seems pretty straightforward.
When the page loads I gather any relevant records from the database and perform a query expression combined with a projection select to create an anonymous type array. When the add new row button is clicked instead of trying to add the row directly or retrieve potentially old data from the database I perform a query expression on the datagrid items themselves and union a new blank item. From there it's just a simple matter of setting the datagrid data source, triggering a databind and updating the update panel.
The only question now is how to identify the rows, particularly new rows that have no database keys yet. I could devise some clever method for generating row ids but I have a feeling that ASP.Net already does this and all I need to do is associate these ids with the anonymous types.