动态生成数据绑定 FormView 字段
我有一个现有的 ASP.NET 3.5 应用程序,其中包含一个 Web 表单,其中包含一个 FormView 控件,用于对域对象执行 CRUD 操作。一个新的需求是向需要在 FormView 中反映的域对象添加一些动态内容。这意味着我需要根据绑定对象即时生成这些控件。
我正在为 FormView 使用 ObjectDataSource,它返回用于绑定的域对象。域对象现在将具有一个属性,该属性返回部分对象的集合,每个部分包含一个问题列表。在表单中间,我需要显示每个部分,其中包含问题列表和允许用户输入答案的文本框。
我可以通过多种不同的方式生成 UI,但我还没有找到一种在页面回发时包含动态字段数据的方法。因为在 FormView 进行数据绑定之前我不会知道“架构”,所以我想知道我在管道中是否为时已晚,无法正确处理回发数据。
生成这些字段以便正确回发数据的最佳方法是什么?
更新
我仍在寻找完成此任务的最佳方法,但我已经找到了一个至少有效的解决方案。简而言之,我在 FormView 的 DataBound 事件处理程序中创建动态内容,因为这是管道中我始终可以获取对 FormView 控件的引用的第一个位置。然后,我按照 Muhammed 的建议,从 Request.Form 集合中提取值,并将它们放入 FormView 的 ItemInserting 和 ItemUpdating 处理程序中的 EventArgs 对象中。
这并不是那么简单,因为每个控件都必须有一个唯一的 ID,然后我可以用它来定位值 - 这还不错。但是,我必须在数据源中实现自定义逻辑,然后将这些值映射到数据绑定对象中。
总而言之,还不错,但肯定不是我想在其他解决方案中重复的事情,所以我希望还有更好的方法。
I have an existing ASP.NET 3.5 application with a webform containing a FormView control used to perform CRUD operations on a domain object. A new requirement adds some dynamic content to the domain object that needs to be reflected in the FormView. This means that I need to generate those controls on-the-fly based on the bound object.
I am using an ObjectDataSource for the FormView that returns the domain object for binding. The domain object will now have a property that returns a collection of Section objects with each Section containing a list of Questions. In the middle of my form, I need to display each section with the list of questions and a textbox that allows the user to input the answer.
I am able to generate the UI a number of different ways but I have yet to find a way that includes data for the dynamic fields when the page is posted back. Because I won't know the 'schema' until the FormView is data-bound, I'm wondering if I'm too late in the pipeline for postback data to be handled properly.
What is the best way for me to generate these fields so the data is posted back correctly?
UPDATE
I'm still looking for the best way to accomplish this task but I've found a solution that at least works. In short, I am creating the dynamic content in the FormView's DataBound event handler because this is the first place in the pipeline that I can always get references to the FormView's controls. Then I follow Muhammed's suggestion and pull the values right out of the Request.Form collection and put them into the EventArgs objects in the FormView's ItemInserting and ItemUpdating handlers.
This isn't so straight forward as each control has to have a unique ID that I can then use to locate the value - which isn't so bad. However, I had to implement custom logic in the data source to then map these values into the data-bound object.
All-in-all, not too bad but certainly not something I'd want to duplicate in other solutions so I'm hoping there's still a better way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您想要从动态生成的字段中获取值时,您应该从
Request.Form
集合中获取它,并将其传递到 FormView 的ItemInserting
事件中。例如,请注意,您的控件应在回发时重新创建才能获取值。
When you want to get the value from the dynamic generated field, you should get it from the
Request.Form
collection and pass it into theItemInserting
event of FormView. e.g.Please note, your controls should be recreated on postback to get the value.