c# - 带有 InitialValue 属性的 ASP.Net RequiredFieldValidator 错误?

标签 c# asp.net validation

我刚刚发现这种奇怪的行为,这是错误还是我遗漏了什么?

<asp:TextBox runat="server" Text="" ID="txtSomething" />
<asp:RequiredFieldValidator 
    ErrorMessage="errormessage" 
    ControlToValidate="txtSomething"
    runat="server"
    Text="(*) Required"
    SetFocusOnError="true"
    EnableClientScript="true"/>

使用这个简单的代码,验证工作正常,即使我写了一个充满空格的字符串

但是如果我像这样添加 InitialValue 属性:

<asp:TextBox runat="server" Text="lol" ID="txtSomething" />
<asp:RequiredFieldValidator 
    ErrorMessage="errormessage" 
    ControlToValidate="txtSomething"
    runat="server"
    Text="(*) Required"
    SetFocusOnError="true"
    EnableClientScript="true"
    InitialValue="lol"/>

注意我添加了 InitialValue="lol"Text="lol"

现在如果我删除文本 lol 验证将被忽略

为什么??

最佳答案

仔细看MSDN说明:

Use this property to specify the initial value of the input control.

Validation fails only if the value of the associated input control matches this InitialValue upon losing focus.

我认为它不言而喻。

关于c# - 带有 InitialValue 属性的 ASP.Net RequiredFieldValidator 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11010562/

相关文章:

c# - 将字符串从 HTTPHandler 返回到发布此 .ashx 文件的页面的最佳方法

c# - 我可以使用什么 JavaScript 代码在新窗口中打开,而不是我之前打开的窗口

validation - Grails:尽管有错误,对象仍被保存

javascript - 用于验证邮政编码/邮政编码的正则表达式

ASP.NET OnClientClick ="return false;"不起作用

c# - 将代码从 .Net 1.1 升级到 2.0/3.5 (C#)

c# - 使用 newtonsoft.json 反序列化 List<AbstractClass>

asp.net - ASP.NET Web API 中的用户身份验证

c# - 从 C# 到 C++ 的转换

javascript - 如何在外部 javascript 文件中获取 asp.net 客户端 ID