In my .aspx page, I’ve a ValidationSummary where I put error messages returned by my Business Layer.
The error messages appears in the summary, ok, but not the “*” that normally appears next to the field =(
In the code behind, I’ve the following code:
CustomValidator cv = new CustomValidator();
cv.ControlToValidate = field.ID;
cv.ErrorMessage = "Error Message";
cv.Text = "*";
cv.IsValid = false;
Page.Validators.Add(cv);
Basically, I want to add the “*” next to each incorrect field, but without creating CustomValidators for each one… is it possible?
The
ErrorMessageproperty will be displayed in theValidationSummarywhile theTextproperty will be displayed where theValidatoris. To meet your requirement, you need to put theCustomValidatorsbeside those controls you want to validate.Since all the validators are generated in code behind, you need to add those validators to a proper position using
Page.Controls.AddAt(int indexer, Control child)becauseCustomValidatoris also a control.When you add the validator beside the textbox, you can get the exact index by following code.
NOTE: if you put the textbox in a PlaceHolder or some containers, you can’t get it by
form1.ControlsbecauseContainer.Controlsreturns the 1st level child controls only.