I have a custom composite control that contains a text box, some validators, as well as several UI components. I am unable to get the client side validation to work at all. The server side validation works fine after the postback. I was ultimately planning to add custom CSS to the TextBox on validation failure by hooking into the validation API, but I can't even get the client side validation running.
Since this control was intended to be generic, the validators are not generated from within the composite control itself but passed in from externally as follows.
<mycontrol:HighlightedTextbox ID="HighlightedTextbox1" runat="server" Label="test" CssClass="generalText" FocusedCssClass="highlightText" ErrorCssClass="errorText">
<validators>
<asp:RequiredFieldValidator ID="required1" runat="server" ErrorMessage="Field is required" EnableClientScript="true" />
</validators>
<prompttemplate><span>this is a prompt</span></prompttemplate>
</mycontrol:HighlightedTextbox>
I have the persistchildren attribute specified (I believe appropriately) so that the validators are in fact added to the Validators property. That appears to be working fine as well.
[PersistChildren(true, true), ParseChildren(true), PersistenceMode(PersistenceMode.InnerProperty)]
public abstract class BaseHighlightedControl<TControl> : CompositeControl
where TControl : Control
I have a derived control that specifies a standard TextBox as TControl as well as exposing a text property but that is really all the derived type does. The composite control relies on the CreateChildControls method to build out the control and configure the validators. This seems like it should be an appropriate in the life-cycle since I have seen examples of composite controls that create validators in the CreateChildControls method.
public List<BaseValidators> Validators { get; private set; }
/// <summary>
/// Create the child controls
/// </summary>
protected override void CreateChildControls()
{
base.CreateChildControls();
this.MainControl.ID = "HighlightControl";
this.PromptTemplate.InstantiateIn(this.Prompt);
this.Prompt.Style.Add(HtmlTextWriterStyle.Display, "inline");
this.FieldLabel.Text = this.Label;
if (!this.DesignMode)
{
this.Controls.Add(this.FieldLabel);
this.Controls.Add(this.MainControl);
this.Controls.Add(this.Prompt);
AddValidators();
}
}
private void AddValidators()
{
foreach (var validator in this.Validators.OfType<BaseValidator>())
{
validator.ControlToValidate = this.MainControl.ID;
validator.ValidationGroup = this.ValidationGroup;
validator.Display = ValidatorDisplay.Dynamic;
this.Controls.Add(validator);
}
}
Again, the server side validation takes place just fine. The Page_Validators collection in javascript doesn't ever contain my validators. Any validators that I add to the markup outside of my control appear in the javascript collection just fine and work appropriately.
What am I doing wrong?