requiredFieldValidator如何自动停止数据库更新?
我之前对 requiredFieldValidator 和类似的印象是,它们会向您显示错误标签并将 Page.IsValid 设置为 false,仅此而已。这意味着他们将剩下的任务(防止使用错误的输入数据)留给您。但今天我意识到,如果您使用DetailsView 将新记录插入数据库,并且使用验证器检查DetailsView 内的文本框,它们会自动阻止数据库更新。
我想知道这是如何在幕后实现的。我猜测它在 Validator.PreRender 事件中中止了页面生命周期,因此无法到达后期的数据库连接?我可能错了。
我正在尝试使用 Reflector 进入RequiredFieldValidator 内部以查看它是如何实现的,但我真的不知道该往哪里看。有人可以给一些提示吗?
My previous impression of RequiredFieldValidator and similar have been that they show you an error label and set Page.IsValid false and that is it. Meaning they leave the rest of the task (preventing the use of wrong input data) to you. But today I have realised say if you use a DetailsView to insert a new record to a database, and you use validators to check the TextBoxes inside the DetailsView, they automatically prevent the database from being updated.
I would like to know how this is implemented behind the scene. I'm guessing it aborted the Page Lifecycle at Validator.PreRender event, so that database connections at later stage could not be reached? I'm probably wrong.
I'm trying to use Reflector to get inside the RequiredFieldValidator to see how it is implemented, but I don't really know where to look. Can someone give some hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
本文详细介绍了 ASP.NET 的验证。
http://msdn.microsoft.com/en-us/library/aa479045.aspx
验证可以是服务器端和/或客户端。如果使用,客户端验证在验证之前不会让用户提交表单。客户端验证是使用 JavaScript 和 DOM 实现的。每个提交按钮都由框架连接,以在回发之前检查验证。
服务器端验证的工作方式有所不同。事件链在页面加载和事件过程调用之间延伸。验证结果已设置,可由任何事件过程解释。
在您的情况下,我猜您有客户端验证 - 它会阻止表单提交到服务器,这就是为什么没有对数据库进行实际更新的原因。
编辑:DetailsView 控件确实支持服务器端验证控件。请点击此链接了解详细信息 http:// msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx#security
This article explains the validation in detail for ASP.NET.
http://msdn.microsoft.com/en-us/library/aa479045.aspx
Validation can be both Server side and/or client side. If used, client side validation doesn't let user submit the form until it is validated. Client side validation is implemented using JavaScript and DOM. Every submit button is wired by the framework to check validation before doing post back.
Server side validation works differently. The event chain is extended between page load and event procedure call. The validation results are set, which can be interpreted by any event procedure.
In your situation I guess you have client validation on - which it prevents Form from being submitted to server and that's why no actual update to database is done.
EDIT: DetailsView control does support server side validation controls. Follow this link for details http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx#security