简体   繁体   English

RequiredFieldValidator不触发

[英]RequiredFieldValidator not firing

I have a simple form with a required Validator on a textbox. 我有一个简单的表单,在文本框上带有必需的验证器。 When I leave the textbox blank and check the Page.isValid it is always true. 当我将文本框留为空白并检查Page.isValid时,它始终为true。 I cannot figure out what I am doing wrong to cause the page to always be true. 我无法弄清楚我做错了什么,导致页面始终正确。

<asp:TextBox ID="txtVendorTracking" runat="server" Width="150px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTxtVendorTracking" 
    InitialValue="Enter Vendor Tracking Nbr" runat="server" 
    ErrorMessage="Vendor Tracking Required." ControlToValidate="txtVendorTracking" 
    ValidationGroup="val" Display="Dynamic">*</asp:RequiredFieldValidator> 

    <asp:Button ID="btnSave" runat="server" Text="Save" Width="100px"  onclick="btnSave_Click" ValidationGroup="val"/>

           protected void btnSave_Click(object sender, EventArgs e)
        {
            Page.Validate(MasterValidation.ValidationGroup);
            if (!Page.IsValid)
            {
                ModalPopupExtender3.Show();
                txtRONbr.Focus();
                return;
            }
          }

Since initial state of the text box is empty, the problem is here: 由于文本框的初始状态为空,因此问题出在这里:

InitialValue="Enter Vendor Tracking Nbr"

Per MSDN : 每个MSDN

Validation fails only if the value of the associated input control matches this InitialValue upon losing focus. 仅当关联的输入控件的值在失去焦点时匹配此InitialValue时,验证才会失败。

Since the initial value of the validator is not an empty string, it treats an empty string as a valid input. 由于验证器的初始值不是空字符串,因此它将空字符串视为有效输入。 TO solve thins you need to decide what whould be an initial state of your text box, and set up validator accordingly. 要解决细化问题,您需要确定谁将成为文本框的初始状态,并相应地设置验证器。 For instance right now you should just remove InitialValue from the markup - it defauls to String.Empty . 例如,现在您应该只从标记中删除InitialValue它默认为String.Empty

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

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