Search code examples
asp.netvalidationcustomvalidator

CustomValidation not firing on second click


I have text box and a button in a gridview. The textbox has validation when the button is clicked. However the validation event never gets fired when the button is clicked a second time.

<asp:GridView ID="ChapterGridView" 
               EnableSortingAndPagingCallbacks="false" 
               AllowSorting="false" 
               AllowPaging="false" 
               runat="server"                            
               AutoGenerateColumns="False" 
               CellPadding="2" 
               ForeColor="#333333"
               GridLines="None"  
               Width="780px" 
               OnRowCommand="ChapterGridView_OnRowCommand" 
               ShowFooter="False" 
               AutoGenerateDeleteButton="true"
               AutoGenerateEditButton="true"
               onrowediting="ChapterGridView_RowEditing"
               onrowdeleting="ChapterGridView_RowDeleting" 
               onrowcancelingedit="ChapterGridView_RowCancelingEdit" 
               onrowupdating="ChapterGridView_RowUpdating"
               onrowupdated="ChapterGridView_RowUpdated"
               DataKeyNames="ChapterId"
               ValidationGroup="ChapterValidation"
               Visible="true">
            <FooterStyle BackColor="#eeeeee" Font-Bold="True" ForeColor="White" />
            <Columns>
                <asp:TemplateField HeaderText="*End Page" HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate><%# Eval("EndPage")%></ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="EndPage" runat="Server" Text='<%# Eval("EndPage") %>'></asp:TextBox>
                        <asp:CustomValidator ID="TotalPagesValidator" ValidationGroup="ChapterValidation" OnServerValidate="TotalPages_ServerValidate" EnableClientScript="false" ErrorMessage="Number of pages in chapter are greater than number of pages in entire publication." Display="None" ControlToValidate="EndPage" runat="server" />
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="EndPage" runat="Server"></asp:TextBox>
                        <asp:CustomValidator ID="TotalPagesValidator2" ValidationGroup="ChapterValidation" OnServerValidate="TotalPages_ServerValidate" EnableClientScript="false" ErrorMessage="Number of pages in chapter are greater than number of pages in entire publication." Display="None" ControlToValidate="EndPage" runat="server" />

                        <asp:Button ID="btnInsert" runat="Server" Text="Insert" CommandName="Insert" UseSubmitBehavior="false" ValidationGroup="ChapterValidation" /></span>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

The same validation for edit works as expected. But not for the Insert button. Any suggestions?


Solution

  • @TrekStir - thanks for pointing me in the right direction.

    The problem was that there was code to make the footer invisible after a row was added. For some reason that was causing the validation code not to fire. I figured out a way to implement the functionality without setting the footer to invisible and this solved the problem.