开发者

How can I set a custom error message from a variable directly from the RequiredFieldValidator?

开发者 https://www.devze.com 2023-03-23 23:12 出处:网络
Tried with this : <asp:RequiredFieldValidator ID=\"rqrNome\" runat=\"server\" Display=\"Dynamic\" ControlToValidate=\"txtNome\"

Tried with this :

<asp:RequiredFieldValidator 
     ID="rqrNome" 
     runat="server" 
     Display="Dynamic" 
     ControlToValidate="txtNome" 
     ErrorMessage="<%= myVar %>">
              &nbsp;*
</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 %>&nbsp;*
</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);
0

精彩评论

暂无评论...
验证码 换一张
取 消