【发布时间】:2014-08-08 06:14:04
【问题描述】:
- VS2013、WebForms、.NET 4.51
我定义了一个 FormView,在我的 EditTemplate 中有如下内容:
<div class="form-group">
<asp:Label ID="lblClientClassification" CssClass="col-md-2 control-label" runat="server" for="cblClientClassifications" Text="Kind"></asp:Label>
<div class="col-md-5">
<asp:CheckBoxList ID="cblClientClassifications" runat="server"></asp:CheckBoxList>
<asp:CustomValidator ID="cfvClientKinds" runat="server" Display="Dynamic" CssClass="label label-danger" ErrorMessage="XXXX" ValidationGroup="Default" OnServerValidate="cfvClientClassifications_OnServerValidate"></asp:CustomValidator>
</div>
然后在后面的代码中:
protected void cfvClientClassifications_OnServerValidate(object aSource, ServerValidateEventArgs aArgs)
{
CustomValidator cvCheckBoxKinds = aSource as CustomValidator;
int checkedCount = 0;
if (cvCheckBoxKinds != null)
{
CheckBoxList cblClientClassifications = GuiClientClassificationsFind();
foreach (ListItem listItem in cblClientClassifications.Items)
{
if (listItem.Selected)
{
checkedCount++;
}
}
if (checkedCount == 0)
{
aArgs.IsValid = false;
cvCheckBoxKinds.ErrorMessage = "Select client kind.";
}
}
}
OnServerValidate 正在触发,我将验证器设置为无效以及设置错误消息(Page.IsValid 也如预期的那样为假)。但是,错误文本未显示。当我查看页面源时,我看到:
<span id="ctl00_cphMainContent_fvData_cfvClientKinds" class="label label-danger" style="display:none;">XXXX</span>
而不是我设置的错误消息以及它不可见的事实。
这里有没有人知道如何追踪这个问题?我看过类似的 SO 问题,但似乎没有一个 cmets 适用。这可能与FormView有关吗?
【问题讨论】:
-
您在这方面取得了进展吗?
-
不,从来没有解决这个问题。
-
我问过你之后很快就解决了我的问题。我在下面分享我的代码作为答案。
标签: asp.net validation twitter-bootstrap webforms visual-studio-2013