Search code examples
asp.netvalidationlistviewrequiredfieldvalidator

How to fix issue with required field validator


I inserted a required field validator in my inserttemplate of my listview. The validator is suppose to check if a value was selected in the listbox when a user clicks insert. It does what is suppose to do however, I have a search button outside the listview that displays the listview columns and when that is clicked the required field validator is triggered. I only want it to trigger when the insert button is clicked within the listview. Is this possible?

Search Button located in ContentTemplate

<div style="padding-top: 5em;  text-align: center; margin-left: auto; margin-right: auto; left: 0; right: 0; width:100%">

            <b style="padding-right: .5em">FormSection </b>
            <asp:TextBox ID="SectionSearchBox" runat="server"></asp:TextBox>
            <b style="padding-left: 1em; padding-right: .5em">SubSection</b>
            <asp:TextBox ID="SubSectionSearchBox" runat="server"></asp:TextBox>
            <b style="padding-left: 1em; padding-right: .5em">Section Item</b>
            <asp:TextBox ID="SectionItemSearchBox" runat="server"></asp:TextBox>
            <asp:Button ID="SearchButton" Height="30px" runat="server" Text="Search" OnClick="SearchButton_Click" />

</div>

Required Field Validator located in content template in the listview's insert template

<span style="color:red">
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required Field!" ControlToValidate="formsection"></asp:RequiredFieldValidator>
</span>
<div style="height: auto; width: 250px; overflow: auto; border: solid; border-color: ActiveBorder" >                                       
    <asp:ListBox ID="formsection" runat="server" DataSourceID="FormSectionDataSource" DataTextField="FormSection" DataValueField="FormSectionID" AppendDataBoundItems="True" SelectedValue='<%# Bind("FormSectionID") %>' Height="150px">
        <asp:ListItem Selected="True" Value=""><- please select -></asp:ListItem>
    </asp:ListBox>

</div>

Solution

  • I figured it out, in the code behind I used.

    SearchButton.CausesValidation = false;
    

    When the button is clicked it does not trigger the validation.