我的验证器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',但它会呈现为纯文本。我觉得我错过了一些非常微不足道的工作。
答案 0 :(得分:2)
可以这样做:
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";