如何更改RequiredFieldValidator 控件返回的内容?

发布于 2024-11-08 01:26:11 字数 322 浏览 1 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(2

柳若烟 2024-11-15 01:26:11

您无法更改 requiredFieldValidator 来更新另一个控件(例如文本框),它只会更新自身或 ValidationSummary(如果页面上有)。

获得所需输出的 ​​2 个选项是:

  1. 使用 CustomValidator 控件
  2. 创建您自己的验证服务器控件,该控件能够更新文本框的 css 类

虽然这些选项很可能都不会为您提供您正在寻找的“纯”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:

  1. Use a CustomValidator control
  2. Create your own validation server control that is capable of updating the css class of a textbox

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.

挽袖吟 2024-11-15 01:26:11

这是更多的工作,但使用 CustomValidator 是可能的。所以你可以通过验证js函数进行更多的控制。

It is more work, but it is possible with CustomValidator. So you have more control through the validation js function.

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