具有内置客户端验证的自定义文本框控件
我的应用程序中有一个文本框,其中有多个验证,例如RequiredFieldValidator、RegexValidation 和CustomValidation。我的页面有几个类似的文本框。所以我只是复制粘贴并更改 id 和 controltovalidate 属性,它就可以工作了。
由于类似的 tbx 也将在另一个页面上使用,因此我认为创建我自己的具有内置验证的自定义 TextBox 控件会很好。
以下是我发现并尝试过的两种方法:
1:从 IValidator 实现,在 Validate Method 中执行我的自定义验证。如图所示: 自我验证文本框 但它没有展示如何实现客户端验证。
2:创建从 TextBox 派生的自定义控件并添加我需要的 asp.net 内置验证器。如图所示:自定义文本框 。我尝试了代码,它可以在服务器/客户端工作。
我喜欢第一种方法,但不知道如何实现客户端验证。我知道我需要一个客户端 js 函数。我能做到。我知道如何使用 Page.ClientScript 类包含我的 js 文件,但不知道如何将所有内容集成在一起并使其工作。
我可以创建一个 UserControl 或上面的第二种方法,但现在我特别希望从自定义控件中学习和实现客户端验证。
我正在使用 Asp.Net 2.0。感谢您的任何建议。
I have a textbox in my application which has multiple validation on it e.g. RequiredFieldValidator, RegexValidation and CustomValidation. My page has several similar textboxes. So I just copy-paste and change id and controltovalidate properties and it is working.
Since similar tbxs are going to be used on another page as well, I think it would be nice to create my own custom TextBox control with built-in validation.
Here are two approaches I have found and tried:
1: Implement from IValidator perform my custom validation in Validate Method. As shown here: Self-Validating TextBox But it does not show how to implement client-side validation.
2: Create custom control that derives from TextBox and add asp.net built-in validators I need. As shown here:Custom TextBox. I tried the code and it works server/client side.
I like the first approach but don't know how to implement client-side validation. I know I need a client-side js function. I can do that. I know how to include my js file using Page.ClientScript class but don't know how to integrate all together and make it work.
I can create a UserControl or the second approach above but for now I am specifically looking to learn and implement client-side validation from custom control.
I am using Asp.Net 2.0. Thanks for any suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,您是对的,您始终可以实现派生自 TextBox 的自定义服务器控件并自动关联几个验证器。但通常您不会创建自定义控件,除非明确需要它,然后通过几个不同的项目。为了拥有客户端验证,您通常需要 JavaScript,但请注意,大多数 ASP.net 验证控件已经内置了客户端验证(即必需的字段验证器、范围验证器等)。 .)。其他的(如自定义验证器)允许挂钩您的自定义 JavaScript。
对我来说听起来更合理的方法是让您的 TextBox 控件与您的 ASP.net 页面/用户控件上的一样,并在运行时动态地将验证器与您的代码关联起来。假设在您的
OnInit
事件中,您调用一个传递 TextBox 列表的函数RegisterRequiredValidators
。我只是在想:这只是一个愚蠢的例子,只是为了解释上下文。理论上,您可以发展这个概念来注册任何类型的验证器。我们在工作中做了类似的事情,将其抽象为“规则”的形式,最终在前端呈现为 ASP.net 验证器。
Well, you're right, you can always implement your custom server control that derives from TextBox and automatically associates a couple of validators. But usually you won't create custom controls as far as it is not explicitly needed and then through several different projects. For having client-side validaton you usually need JavaScript, but note that most of the ASP.net validation controls have their client-side validation already built-in (i.e. required field validators, range validators,...). Others (like the custom validator) allow to hook in your custom javascript.
An approach that sounds more reasonable to me is to have your TextBox controls as they are on your ASP.net page/usercontrol and to associate the validators from your code dynamically at runtime. Say in your
OnInit
event, you call a functionRegisterRequiredValidators
passing in a list of TextBoxes. I'm just thinking aloud:That's just a stupid example, just to explain the context. In theory you can evolve this concept to register any kind of validators. We do something similar at work, by abstracting this in form of "rules" which ultimately are being rendered as ASP.net validators on the front-end.