0

I am having an issue where I have a custom validator on each row in a gridview, and a summary at the bottom of the panel the gridview sits in.

The custom validator fires when it is supposed and I set args.IsValid=false and assign an appropriate error message, but on my screen I don't see either the text of my custom validator, or the errorMessage in my summary. In fact I don't see my summary at all.

I have looked and I can't see why this would be happening, as I have a used near-identical setup elsewhere and it worked fine.

Markup:

<asp:GridView ID="gvConsolidatedPeriods" runat="server" OnRowDataBound="gvConsolidatedPeriods_OnRowDataBound" SkinID="alternativeRows"
                    AutoGenerateColumns="false" Width="99%" OnDataBound="gvConsolidatedPeriods_OnDataBound" OnRowEditing="gvConsolidatedPeriods_OnRowEditing"
                    OnRowCancelingEdit="gvConsolidatedPeriods_OnRowCancellingEdit" OnRowDeleting="gvConsolidatedPeriods_OnRowDeleting" ValidationGroup="ConsolidatedPeriodValidationGroup">
                   <Columns>
                        <asp:TemplateField HeaderText="Period">
                            <HeaderStyle Font-Bold="True" HorizontalAlign="Left" Width="250px" />
                            <ItemStyle HorizontalAlign="Left" Width="250px" />
                            <ItemTemplate>
                                <asp:Label ID="lblPeriod" runat="server" />
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:HiddenField ID="hdPeriodId" runat="server" />
                                <asp:Label ID="lblPeriod" runat="server" Visible="false"/>
                                <asp:TextBox ID="txtFromDate" runat="server" CssClass="datepicker" Visible="false"/>
                                <br /> 
                                <asp:Label ID="lblTo" runat="server" Visible="false" Text=" to " CssClass="label" />
                                <br />
                                <asp:TextBox ID="txtEndDate" runat="server" CssClass="datepicker" Visible="false"></asp:TextBox>
                                &nbsp;&nbsp;&nbsp;&nbsp;abc
                                <asp:CustomValidator ID="cvConsolidatedPeriodDates" runat="server" ValidationGroup="ConsolidatedPeriodValidationGroup" Text="*"
                                    SetFocusOnError="True" ErrorMessage="" OnServerValidate="cvConsolidatedPeriodDates_OnServerValidate" ValidateEmptyText="true" Visible="true" Style=""/>s
                            </EditItemTemplate>
                        </asp:TemplateField>

...

Server Validate Code:

        var rowToSave = gvConsolidatedPeriods.Rows[gvConsolidatedPeriods.EditIndex];
        if (rowToSave != null)
        {
            var txtfromDate = rowToSave.FindControl("txtFromDate") as TextBox;
            var txtendDate = rowToSave.FindControl("txtEndDate") as TextBox;
            var cvConsolidatedPeriodValidator =
                rowToSave.FindControl("cvConsolidatedPeriodDates") as CustomValidator;
            var test = source as CustomValidator;

            if (txtfromDate != null && txtendDate != null && txtfromDate.Visible)
            {
                if (String.IsNullOrEmpty(txtfromDate.Text))
                {
                    args.IsValid = false;
                    test.ErrorMessage = "Period Dates are required";
                    return;
                }

...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

0 Answers0