我遇到了一个问题,我在 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>
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;
}
...