具有内置客户端验证的自定义文本框控件

发布于 2024-10-14 21:39:45 字数 870 浏览 2 评论 0原文

我的应用程序中有一个文本框,其中有多个验证,例如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 技术交流群。

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

发布评论

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

评论(1

怎会甘心 2024-10-21 21:39:45

嗯,您是对的,您始终可以实现派生自 TextBox 的自定义服务器控件并自动关联几个验证器。但通常您不会创建自定义控件,除非明确需要它,然后通过几个不同的项目。为了拥有客户端验证,您通常需要 JavaScript,但请注意,大多数 ASP.net 验证控件已经内置了客户端验证(即必需的字段验证器、范围验证器等)。 .)。其他的(如自定义验证器)允许挂钩您的自定义 JavaScript。

对我来说听起来更合理的方法是让您的 TextBox 控件与您的 ASP.net 页面/用户控件上的一样,并在运行时动态地将验证器与您的代码关联起来。假设在您的 OnInit 事件中,您调用一个传递 TextBox 列表的函数 RegisterRequiredValidators。我只是在想:

public override void OnInit(...)
{
   ...
   RegisterRequiredValidators(
      txtFirstname, 
      txtSurname, 
      txtAge
   }
   ...
}

public void RegisterRequiredValidators(params Control[] textBoxes)
{
   //execute the logic of creating and attaching validators
}

这只是一个愚蠢的例子,只是为了解释上下文。理论上,您可以发展这个概念来注册任何类型的验证器。我们在工作中做了类似的事情,将其抽象为“规则”的形式,最终在前端呈现为 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 function RegisterRequiredValidators passing in a list of TextBoxes. I'm just thinking aloud:

public override void OnInit(...)
{
   ...
   RegisterRequiredValidators(
      txtFirstname, 
      txtSurname, 
      txtAge
   }
   ...
}

public void RegisterRequiredValidators(params Control[] textBoxes)
{
   //execute the logic of creating and attaching validators
}

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.

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