i have textbox controls that are created dynamically in the ASP.NET. I want to add dynamically custom validators for these controls. These validators should be run on the client side. I have the following code snippet:
protected override void InitDynamicControls()
{
registerScript();
CustomValidator cv = new CustomValidator();
cv.ErrorMessage = "error";
cv.Display = ValidatorDisplay.Dynamic;
cv.ControlToValidate = TextField.ID;
cv.ValidationGroup = "ConfigurationValidation";
cv.ClientValidationFunction = "ConfigurationOption_ClientValidate";
base.Controls.Add(cv);
}
private void registerScript()
{
System.Text.StringBuilder script = new System.Text.StringBuilder();
script.Append("<script language=\"javascript\">\n");
script.Append("function ConfigurationOption_ClientValidate(source, arguments) \\{arguments.IsValid = false;\\}\n");
script.Append("</script>\n");
Type type = this.GetType();
if (!Page.ClientScript.IsClientScriptBlockRegistered(type, "ConfigurationOption_ClientValidate"))
Page.ClientScript.RegisterClientScriptBlock(type, "ConfigurationOption_ClientValidate", script.ToString());
}
The method in the javascript will contain the logic. Method InitDynamicControls() is loaded in Page_Load method. When I click the button on the page the validator doesn’t run.
can you help me what can be wrong?
thanks
Try this:
Or even better use this: