I've dynamically added a RequiredFieldValidator control to a textbox in a formview. The first time the page loads I can see the script block hooking up the javascript function to the control...
When the formview's cancel button is clicked and post back occurs, the page is reloaded and the code to dynamically add the validator is run again. The script above is not added though so client validation can't occur if update is then clicked.Does anyone know why this happens? What is different in the chain of events when cancel is clicked to those events on a page load?I've been looking at the RequiredFieldValidator's AddAttributesToRender method and to confuse me more, when I watch RenderUplevel it always comes back false - even on page load so how is the script ever added??
Thanks
Matt
Code:
<script type="text/javascript">
//<![CDATA[
var FormView1_validator2 = document.all ? document.all["FormView1_validator2"] : document.getElementById("FormView1_validator2");
FormView1_validator2.controltovalidate ="FormView1_NameTextBox";
FormView1_validator2.errormessage ="Error";
FormView1_validator2.evaluationfunction ="RequiredFieldValidatorEvaluateIsValid";
FormView1_validator2.initialvalue = "";
//]]>
</script>
Code:
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
if (base.RenderUplevel)
{
string clientID = this.ClientID;
HtmlTextWriter writer2 = base.EnableLegacyRendering ? writer : null;
base.AddExpandoAttribute(writer2, clientID, "evaluationfunction", "RequiredFieldValidatorEvaluateIsValid", false);
base.AddExpandoAttribute(writer2, clientID, "initialvalue", this.InitialValue);
}
}
Matt