ASP.NET自定义验证器和validationSummary不会出现错误消息,但仍会在gridview中触发验证

时间:2012-04-27 04:07:54

标签: asp.net customvalidator validationsummary

我遇到一个问题,我在gridview中的每一行都有自定义验证器,并且在gridview所在的面板底部有一个摘要。

自定义验证器在应用时触发,我设置了args.IsValid = false并分配了相应的错误消息,但在我的屏幕上,我看不到自定义验证器的文本或我的摘要中的errorMessage。事实上,我根本没有看到我的摘要。

我看了,我不明白为什么会发生这种情况,因为我在其他地方使用了几乎相同的设置并且工作正常。

标记:

<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>

...

服务器验证码:

        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;
                }

...

0 个答案:

没有答案