如何使用ValidationAttribute在选项页面用户输入? [英] How to use ValidationAttribute for user input in options pages?
问题描述
我在写一个Visual Studio扩展。我有一个C#类重presenting的选项页。一个选项,再$ P $由公共财产psented,像这样的:
I'm writing a Visual Studio extension. I have a C# class representing an options page. An option is represented by a public property, like this:
public class OptionsPageGeneral : DialogPage
{
[Range(1, 100)]
[Category("Misc")]
[DisplayName("Integer option")]
public string IntOption { get; set; }
...
}
我试图使用 RangeAttribute
<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.aspx\"相对=nofollow>验证属性来限制用户的输入。然而,对于给定的code,用户仍然可以输入任何值,不仅从[1; 100]范围内。
I'm trying to use the RangeAttribute
validation attribute to restrict user input. However, for the given code, user still can input any value, not only from [1; 100] range.
我见过ValidationAttribute使用的很多例子,但全部为ASP.NET MVC项目。难道这个属性只适用于这方面?
I've seen lots examples of ValidationAttribute usage, but all for ASP.NET MVC projects. Is it true this attribute is only applicable in that context?
反正,我怎么能验证用户输入的选项页做了什么?我知道我可以简单地重写属性设置
方法,但是验证特性要求少得多code编写,并可以为类似的特性被重用。
Anyway, how can I validate user input done in options page? I know I can simply override the property set
method, but validation attributes require much less code to write and can be reused for the similar properties.
推荐答案
我没有关于如何使用选项页的想法,但如果你想要,而不是之间1-100的范围内,你可以使用常规的前pression 范围
。
I dont have idea on how to use options page, but you can use a regular expression if you want a range between 1-100 instead of Range
.
[Category("Misc")]
[DisplayName("Integer option")]
[RegularExpression(@"[0-9]{2}")]//This will only allow you to enter only 2 digits
public string IntOption { get; set; }
这篇关于如何使用ValidationAttribute在选项页面用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!