使用手动数据绑定检索 Formview 中的键和 NewValues

发布于 2024-09-11 04:28:06 字数 619 浏览 3 评论 0原文

我在代码文件中将 Formview 绑定到通用列表。现在,在编辑记录时,我希望从 ItemUpdating 事件处理程序方法的 FormViewUpdateEventArgs 参数中访问 Keys 和 NewValues。

从我现在在互联网上尝试和搜索的结果来看,我知道只有在标记页面上将 Formview 设置为数据源控件时,更新的值才可用,否则它们将为空。这是真的吗?

其次,此时我将 sender 对象转换为 formview,并使用 FindControl 方法查找和检索控件中存在的值来单独填充每个对象属性。这是完成这项任务的最佳方式吗?

举个例子,这就是我正在做的 atm:

FormView currentForm = (FormView)sender;
ListObject.ID = new Guid(((HiddenField)(currentForm.FindControl("hdnID"))).Value);
ListObject.Name = ((TextBox)(currentForm.FindControl("txtName"))).Text;

感谢大家的帮助!

I have a Formview binded in the code file to a generic list. Now, upon editing of a record, I wish to access the Keys and NewValues out of the FormViewUpdateEventArgs parameter of the ItemUpdating event handler method.

From what I've tried and searched over the internet as of now, I've come to know that updated values are only available if the Formview is set a data source control on the markup page else they'd be null. Is this true?

Secondly, at this moment I am casting the sender object to formview and individually filling each object property by using FindControl method to find and retrieve values present in the controls. Is this the best way to do this task?

As an example, this is what I am doing atm:

FormView currentForm = (FormView)sender;
ListObject.ID = new Guid(((HiddenField)(currentForm.FindControl("hdnID"))).Value);
ListObject.Name = ((TextBox)(currentForm.FindControl("txtName"))).Text;

Thanks for the help fellas!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说谎友 2024-09-18 04:28:06

根据您所做的事情,我建议您不要使用 FormView。 FormView 在处理数据源时非常出色,但在按照您的方式处理手动绑定数据时会失败,您基本上覆盖并手动构建表单,并且仅创建 HTML 表单和 ASP.Net 服务器端控件会更简单。

FindControl 是一项昂贵的操作,并且可能变得笨拙。在数据循环期间简单地为服务器端控件分配一个值会更快。

或者,按照建议,使用 ObjectDataSource 并以这种方式将数据绑定到 FormView。

Based on what your doing I would suggest you not use a FormView. FormView's are brilliant when working with datasources, but fail, when dealing with manual bound data the way you are, your basically overriding and manually building the form, and it would be simpler to just create a HTML form and ASP.Net Server Side Controls.

FindControl is an expensive operation and can become unwieldily. Simple assigning a value during the loop of your data to a server side control will be faster.

Alternatively as suggested, use a ObjectDataSource and bind your data to the FormView in that way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文