添加的RequiredFieldValidator运行 [英] Adding RequiredFieldValidator runtime

查看:88
本文介绍了添加的RequiredFieldValidator运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET用户控件(一个注册表单),我需要添加一个RequiredFieldValidator运行。但是,它不工作。
这里是我添加验证器。

I have a ASP.NET usercontrol ( a sign-up form ), and I need to add a requiredFieldValidator runtime. However, it's not working. Here is how I add the validator.

System.Web.UI.WebControls.RequiredFieldValidator nameValidator = new System.Web.UI.WebControls.RequiredFieldValidator();
nameValidator.ControlToValidate = "SignUpName";
nameValidator.ErrorMessage = "You must provide your name";
nameValidator.ID = "nameValidator";
nameValidator.Display = ValidatorDisplay.Dynamic;
this.Page.Validators.Add(nameValidator);

我是什么失踪?

-------- ---------编辑

-------- EDIT ---------

我从来没有发现一个解决方案。我使用的CustomValidator和人工手动操作的所有验证达到我的目标。

I never found a solution for this. I achieved my goal by using a CustomValidator and doing all the validation manually.

推荐答案

如果您要添加控制,包括动态验证,那么你需要将它们添加在Page_Init不是Page_Load中,否则他们没有得到正确添加到控件树,即他们不T参加的ViewState

If you are adding controls including validators dynamically then you need to add them on Page_Init not Page_Load otherwise they don't get added to the control tree correctly i.e they don't participate in ViewState

你最好使用一个自定义的验证,而不是动态的,虽然增加了必要的验证。在asp.net中动态控制已经没有什么,但对我的麻烦。如果有另一种解决方案,我会避免。

You're probably better using a custom validator rather than dynamically adding a required validator though. Dynamic controls in asp.net have been nothing but trouble for me. I would avoid if there is another solution.

这篇关于添加的RequiredFieldValidator运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆