Tried with this :
<asp:RequiredFieldValidator
ID="rqrNome"
runat="server"
Display="Dynamic"
ControlToValidate="txtNome"
ErrorMessage="<%= myVar %>">
*
</asp:RequiredFieldValidator>
but seem开发者_如何学运维s that this is not possible. I'll get rid about go to the .ascx.cs and use for each validator
rqrNome.ErrorMessage = myVar;
sometimes too many variables to check. I'd like to put directly on the .ascx.
Is it possible?
I think I cannot remove runat="server"
tag from the RequiredFieldValidator
Try this instead:
<asp:RequiredFieldValidator
ID="rqrNome"
runat="server"
Display="Dynamic"
ControlToValidate="txtNome">
<%= myVar %> *
</asp:RequiredFieldValidator>
Update:
Ok, well how about using the above to set the text via your 'myVar'. Then include the following linq query to iterate all of the requiredfieldvalidtors on the page;
var requiredFieldValidators =
from validators in this.Page.Controls.Cast<Control>()
where validator is RequiredFieldValidator
select (RequiredFieldValidator)validator;
Then iterate all the validators setting their ErrorMessage to become equal to their text property which has already been set correctly.
requiredFieldValidators.ToList().ForEach(c => c.ErrorMessage = c.Text);
精彩评论