1

我的验证器 ErrorMessage 中需要一些动态文本。

这是我正在尝试做的,但它不起作用:

<td>
  <asp:TextBox runat="server" ID="tbIncome_A_3" MaxLength="12"></asp:TextBox>
  <asp:CompareValidator ID="comvIncome_A_3" runat="server" ControlToValidate="tbIncome_A_3" Display="Dynamic" Operator="DataTypeCheck" Type="Currency"
    ErrorMessage="*Value entered for <%=DateTime.Today.Year - 3 %> Income must be a dollar value.">
    <span class="alert">*</span>
  </asp:CompareValidator>
</td>
...
<div>
  <asp:ValidationSummary ID="vs" runat="server" CssClass="alert alert-danger alert-dismissible" HeaderText="This page has been saved, but it's not complete." ForeColor="" EnableClientScript="false" EnableViewState="false" />
</div>

我希望<%=DateTime.Today.Year - 3 %>在 ValidationSummary 中呈现为“2014”,但它却呈现为纯文本。我觉得我错过了一些非常微不足道的东西来完成这项工作。

4

1 回答 1

1

可以这样做:

ErrorMessage='<%# "*Value entered for " + DateTime.Today.AddYears(-3).Year + " Income must be a dollar" %>'

但是,您需要调用您DataBind();Page_Load页面。所以既然你确实需要修改后面的代码,为什么不从那里设置错误消息......(那时不需要DataBind())

tbIncome_A_3.ErrorMessage = "*Value entered for " + DateTime.Today.AddYears(-3).Year + " Income must be a dollar";
于 2017-03-31T17:32:26.583 回答