2

I have many asp.net validation controls in my aspx page and I dont want to hard code there error message.

I want to put a variable in C#, set error message there and set text properties of required filed validates. Unfortunately I dont know how to do it.

Please guide me is it possible and how it is possible ?

1 Answer 1

2

An ASP.Net Validation Control typically derives from BaseValidator, which has both a Text property as well as a ErrorMessage property.

You can set either of these in your code behind. So given this validator:

<asp:RequiredFieldValidator id="myValidator"
   ControlToValidate="TextBox1"
   Display="Static"
   Width="100%" runat=server />

In your code behind you can do this:

var errorText = "Some Text";
var errorMessage = "It's broken yo!";

myValidator.Text = errorText;
myValidator.ErrorMessage = errorMessage;

The Text property is what shows up directly in the control, and the ErrorMessage is what get's shown in the ValidationSummary

Sign up to request clarification or add additional context in comments.

3 Comments

Josh I want to just set it by a variable not control by control for all controls. Is there a way ? can it be done like set text of a variable and set validation control's text property to it ? plz guide
@user576510 - I'm not completely sure I understand what you want at this point, but I've updated the answer to show how you could initialize the control properties from a variable.
Josh I meant to set the text from a C# variable but in aspx (code) page. plz advice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.