简体   繁体   English

为什么我的RequiredFieldValidators没有停止发布表单?

[英]Why aren't my RequiredFieldValidators stopping the form from posting?

Per the erroneously indicated "duplicate", no validation occurs when the button is clicked. 根据错误指示的“重复”,单击按钮时不会发生验证。 The postback proceeds unimpeded by validation behavior. 回发过程不受验证行为的影响。 I thought I was clear on this when I said "the page is still posting" but apparently I was wrong. 当我说“页面仍在发布”时,我以为我很清楚,但显然我错了。

This shouldn't be so confusing but I'm lost. 这不应该如此令人困惑,但是我迷路了。

I've got several RequiredFieldValidator controls on my page in which I'm not specifying an InitialValue so the validators should fail to validate if the ControlToValidate 's value is empty ( according to documentation ). 我的页面上有几个RequiredFieldValidator控件,在这些控件中我没有指定InitialValue因此验证器应该无法验证ControlToValidate的值是否为空( 根据文档 )。

The page is still posting, however, even with empty values (although Page.IsValid returns false in this case). 即使该页面具有空值,该页面仍在发布(尽管在这种情况下, Page.IsValid返回false)。

<div class="form-group">
    <label class="col-md-3 control-label">Registered Name</label>
    <div class="col-md-9">
        <asp:TextBox runat="server" ID="RegisteredName" CssClass="form-control" />
        <asp:RequiredFieldValidator runat="server" ID="RegisteredNameRequiredFieldValidator" ControlToValidate="RegisteredName" ErrorMessage="*" ForeColor="Red" />
    </div>
</div>

<div class="col-md-12">
    <asp:Button runat="server" ID="Save" Text="Save" CssClass="btn btn-primary" OnClick="Save_Click" CausesValidation="true" />
</div>

Here's the backend code: 这是后端代码:

protected async void Save_Click(object sender, EventArgs e)
{
    try
    {
        if (Page.IsValid)
        {
            // Save the form data to the database.
            var company = await SaveCompany();
            await SaveCompanyMeta(company);
            await SaveBankingDetails(company);
            await SaveContacts(company);
            await SaveShareholders(company);

            // Indicate that the operation processed successfully.
            var msg = $"Company details for new company {company.RegisteredName} have been saved. Redirecting to company list...";
            ShowSnackbar(msg, "/Companies");
        }
    }
    catch (Exception ex)
    {
        ShowSnackbar(ex.Message);
    }
}

Now I'm more than happy to leave this as-is considering it still prevents corrupt data from being posted to the database, but the fact that it doesn't indicate anything on the UI is what I'm having a problem with. 现在,我很乐意将其保留为原样,因为它仍然可以防止将损坏的数据发布到数据库中,但是事实是它在UI上没有任何指示,这是我遇到的问题。

I've gone and built this more or less according to asp.net-tutorials.com but I'm not having any luck getting it to work correctly. 我已经根据asp.net-tutorials.com或多或少地构建了这个组件,但是我没有运气让它正常工作。 Have I missed something? 我错过了什么吗? Why isn't this showing any errors on the page when validation fails? 验证失败时,为什么在页面上没有显示任何错误?

Worked around by adding the HTML5 required attribute to the fields that I want to validate. 通过将HTML5 required属性添加到我要验证的字段中来解决。 Either on its own or along with the logic already in place, the form submission is now not happening and the UI indicates that the field is required: 不管是单独还是与已经存在的逻辑一起,现在都不会进行表单提交,并且UI指示必须填写该字段:

<asp:TextBox runat="server" ID="RegisteredName" CssClass="form-control" required />

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

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