页面上的两个单独的搜索表单要求在验证时填写彼此的字段

时间:2016-09-15 18:18:13

标签: c# forms validation input submit

我的页面有两个单独的搜索表单,一个用于按项目ID搜索,另一个用于按关键字搜索。它看起来像这样:

search forms

我从最上面开始,我现在添加底部的一个。底部基本上是第一个的复制和粘贴(两者都是Sitecore中的组件)。这是每个代码:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SearchByItemNumber.ascx.cs" Inherits="Bayer.CropScience.SC.Internet.US.Website.layouts.Bayer_CropScience.Scope.Country_United_States_Internet.RewardsCatalog.SearchByItemNumber" %>

<div class="form search-item">
    <div class="clearfix">
        <h3>
            <label class="searchByItemNumber" for="item-num">Know your item number?</label></h3>
        <div class="input-wrapper search-by-itemnumber-length">
            <asp:TextBox runat="server" ID="txtSearchItemNumber" CssClass="search-by-itemnumber-length" placeholder="Search by Item Number" />
            <asp:RequiredFieldValidator runat="server" ID="rfvSearchItemNumber" ControlToValidate="txtSearchItemNumber" ValidationGroup="searchCatalog" ErrorMessage="Please fill out this field." CssClass="error"></asp:RequiredFieldValidator>
        </div>
        <asp:Button runat="server" Text="Go" CssClass="small" OnClick="Search" ValidationGroup="searchCatalog" />
    </div>
</div>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SearchByDescription.ascx.cs" Inherits="Bayer.CropScience.SC.Internet.US.Website.layouts.Bayer_CropScience.Scope.Country_United_States_Internet.RewardsCatalog.SearchByDescription" %>

<div class="form search-item">
    <div class="clearfix">
        <h3>
            <label class="searchByKeywords" for="item-keywords">Search by keywords</label></h3>
        <div class="input-wrapper search-by-keywords">
            <asp:TextBox runat="server" ID="txtSearchKeyword" CssClass="search-by-keywords" placeholder="Search by keywords" />
            <asp:RequiredFieldValidator runat="server" ID="rfvSearchKeyword" ControlToValidate="txtSearchKeyword" ValidationGroup="searchCatalogKeywords" ErrorMessage="Please fill out this field." CssClass="error"></asp:RequiredFieldValidator>
        </div>
        <asp:Button runat="server" Text="Go" CssClass="small" OnClick="KeywordSearch" ValidationGroup="searchCatalogKeywords" />
    </div>
</div>

问题在于,当我填写项目编号并单击Go时,我会在关键字框下面收到验证错误消息“请填写此字段”,反之亦然。我该如何阻止这种情况发生?

编辑 - 当我按下“输入”时发生验证错误,但是当我点击“开始”按钮时发生错误

编辑2 - 这是一个javascript问题,按Enter键导致两个字段都有效。我用自定义onkeypress函数覆盖了它

1 个答案:

答案 0 :(得分:3)

您需要拥有不同的 ValidationGroups 。您在所有字段上使用ValidationGroup="searchCatalog"。 将唯一验证组分配给您希望一起验证的字段。