The easiest way is to set the DropDownList AutoPostBack property to true and handle the SelectedIndexChanged event. Then you can enable / disable the validator there.
Another approach is to use CustomValidator . This validator is independent of one control. You must write the verification rules yourself. For example, ClientValidationFunction :
<script type="text/javascript" > function ClientValidate(source, arguments) { var txt = document.getElementById('TextBox1'); var ddl = document.getElementById('DropDownList1'); var decision = ddl.options[ddl.selectedIndex].text; if(decision=='Yes'){ arguments.IsValid = txt.value.length > 0; }else{ arguments.IsValid = true; } } </script> <asp:DropDownList id="DropDownList1" runat="server"> <asp:ListItem Selected="True">Yes</asp:ListItem> <asp:ListItem Selected="False">No</asp:ListItem> </asp:DropDownList> <asp:TextBox id="TextBox1" runat="server" /> <asp:Button ID="BtnSubmit" runat="server" Text="Submit" /> <asp:CustomValidator id="CustomValidator1" ValidateEmptyText="true" ControlToValidate="TextBox1" ClientValidationFunction="ClientValidate" OnServerValidate="ServerValidation" Display="Static" ErrorMessage="Please enter text!" runat="server"/>
Remember that always run OnServerValidate , because you should not rely only on javascript (you can disable it). This is easy:
void ServerValidation(object source, ServerValidateEventArgs args){ args.IsValid = DropDownList1.SelectedIndex == 1 || TextBox1.Text.Length > 0; }
Vb.net
Protected Sub ServerValidation(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) args.IsValid = DropDownList1.SelectedIndex = 1 OrElse TextBox1.Text.Length > 0 End Sub
source share