如何更改RequiredFieldValidator 控件返回的内容?
我对 ASP.NET 还很陌生,但我正在尝试验证表单。我已经进行了验证,但我想更改它的显示方式。我使用的是RequiredFieldValidator 控件,它生成一个带有ErrorMessage 属性的span 标记。根据我设置某些属性的方式,span 标记要么隐藏,要么使用内联 CSS 显示(display:none、visibility:hidden 等)。我不想让验证返回一个跨度,而是希望根据验证结果更改 TextBox 控件的 CssClass。我不希望标记中的任何地方出现与验证相关的范围,即使使用 display:none 也是如此。
这可能吗?我从哪里开始实现这个目标?
谢谢!
I'm pretty new to ASP.NET, but I am trying to validate a form. I've got the validation working, but I want to change how it displays. I'm using the RequiredFieldValidator control which generates a span tag with the ErrorMessage attribute inside. Depending on how I set some of the attributes, the span tag is either hidden or displayed with inline CSS (display:none, visibility:hidden, etc). Rather than have the validation return a span, I'd like to have the CssClass of the TextBox control change based on the result of the validation. I don't want a validation related span anywhere in my markup, even with display:none.
Is this possible? Where do I start to achieve this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法更改 requiredFieldValidator 来更新另一个控件(例如文本框),它只会更新自身或 ValidationSummary(如果页面上有)。
获得所需输出的 2 个选项是:
虽然这些选项很可能都不会为您提供您正在寻找的“纯”HTML 代码,而无需任何额外的内容span 或 div 标签,只要您愿意投入时间编写 JavaScript(或 jQuery),它们将满足您的其他要求。但是,考虑到可用的替代方案(主要是通过某些框架仅进行客户端验证),您最好还是坚持使用 ASP.NET 验证,因为它可以在服务器端强制执行验证。
第二个选项更具挑战性,但服务器控件可以在项目中的任何页面上使用,也可以包装在 DLL 中以部署在多个项目中。对于 CustomValidator,如果您想在多个控件上复制功能,则需要进行一些复制和粘贴,这可能会很难维护。
有关如何使用 jQuery 和自定义服务器控件来获取所需结果的实际示例,请转到 折叠椅仓库(您需要先将商品添加到购物车才能到达那里)。当您提交表单而不填写表单时,无效的文本框将被分配新的 css 类,这些类会更改其背景颜色。但是,由于它们是服务器控件,因此无论是否启用 JavaScript,该功能都将起作用。
You cannot change the RequiredFieldValidator to update another control like a text box, it will only update itself or a ValidationSummary if there is one on the page.
2 options you have to get your desired output are:
While most likely neither of these options will give you the "pure" HTML code you are looking for without any extra span or div tags, they will meet your other requirements provided you are willing to put in the time to write the JavaScript (or jQuery). However, considering the alternatives that are available (mostly client-side only validation via some framework), you are still better off sticking with ASP.NET validation because it can enforce the validation on the server side.
The second option is more challenging, but a server control can be used on any page in your project or wrapped in a DLL to be deployed in multiple projects. With a CustomValidator there is some copy and paste if you want to duplicate the functionality on multiple controls, which can get difficult to maintain.
For a real-world example of how you can use jQuery and custom server controls to get the results you are looking for, go to the checkout page on Folding Chair Depot (you will need to add an item to the cart first to get there). When you submit the form without filling it out, the invalid textboxes are assigned new css classes which change their background color. However, because they are server controls, the functionality will work whether JavaScript is enabled or not.
这是更多的工作,但使用 CustomValidator 是可能的。所以你可以通过验证js函数进行更多的控制。
It is more work, but it is possible with CustomValidator. So you have more control through the validation js function.