Return to the menu   Select another DES Module

Demonstrates the Enabler property, which enables the validator based on the state of other controls

There are times when a Validator control should be disabled. For example, don’t validate a textbox because an associated checkbox is unmarked or a DropDownList does not have the right value. Use the Enabler property on the Validator control to set up rules to enable and disable it. The rules are actually Condition objects, like those inside of each Validator and used in the MultiConditionValidator.

By default, the Enabler property is set to “None”, where it doesn’t disable the control. You can set it to any Condition object.

Be aware of these issues when using the Enabler property:

  • By default, Validators will not evaluate when the associated data entry control is hidden or disabled. If you need to allow validation when hidden or disabled, you must set the PageManager.AutoDisableValidators property to false. Then assign the Enabler to the appropriate conditions: VisibleCondition, EnabledCondition, or a MultiCondition that contains both.
  • Most Conditions have a property called EvaluateOnClickOrChange which defaults to true. Change it to false when using it in an Enabler. If its left true, the control it evaluates will cause the validator to evaluate and it will have its appearance changed when there is an error and PageManager.ChangeStyleOnControlsWithError is used.
  • Do not use this to detect a control whose Visible property is set to false. Such a control does not create HTML for the client-side to use. Instead, set the Condition’s Enabled property to false when the control is Visible=false.

See the Source Code section for the ASP.NET markup syntax.


Controls

Leave the textbox blank and click Submit. Then mark the checkbox and again click Submit. The page will submit only if the checkbox is unmarked.





Source Code (C#)

<script runat="server">
protected void Button1_Click(object sender, EventArgs e) { if (PeterBlum.DES.Globals.WebFormDirector.IsValid) { // save your data here } }
</script> Leave the textbox blank and click Submit. Then mark the checkbox and again click Submit. The page will submit only if the checkbox is unmarked. <br/><br/> <asp:CheckBox ID="CheckBox1" runat="server" Text="When marked the RequiredTextValidator is enabled." /><br/><br/> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <des:RequiredTextValidator ID="RequiredTextValidator1" runat="server" ControlIDToEvaluate="TextBox1" ErrorMessage="Required"> <EnablerContainer> <des:CheckStateCondition ControlIDToEvaluate="CheckBox1" EvaluateOnClickOrChange="False" /> </EnablerContainer> </des:RequiredTextValidator> <br/> <des:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />

Return to the menu   Select another DES Module