简体   繁体   English

ASP.net:搜索页面上的所有验证控件

[英]ASP.net: Searching for all validation controls on a page

I want to search an ASP.net form for all types of validation controls and programmatically add some attributes to them such as ForeColor. 我想在ASP.net表单中搜索所有类型的验证控件,并以编程方式向其中添加一些属性,例如ForeColor。 Can someone point me in the right direction on this? 有人可以为此指出正确的方向吗?

Cheers and thanks Stackers :) 欢呼和感谢堆垛机:)

I think you may get to something using the method Page.GetValidators() 我认为您可以使用方法Page.GetValidators()

It returns a collection of IValidator so you would need to cast it to the appropriate class 它返回IValidator的集合,因此您需要将其转换为适当的类

Here you have a sample usage. 这里有一个示例用法。

That turned out to be easier than I first thought thanks to Claudio's tips: 事实证明,这比我最初想到的要容易,这要归功于Claudio的提示:

    foreach (IValidator cValidator in Page.GetValidators(null))
    {
        BaseValidator bv = (cValidator as BaseValidator);
        bv.CssClass = "Error";
        bv.Display = ValidatorDisplay.Dynamic;
        bv.ForeColor = System.Drawing.Color.Empty;
    }

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM