我刚刚发现这种奇怪的行为,这是错误还是我遗漏了什么?
<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/