39083

Validation of another controls based on the dropdown choice

Question:

I am facing a problem for validating the dependency controls. How to validate the another controls which is based on the values of dropdownlist (combo box). Please see the below example. For Example : A dropdown has two values A and B. If user selects "A", then rest of the fields should become required fields and if user selects value “B” from the dropdown, then all other fields should become non-mandatory fields.

Note : i am using the devexpress gridview with default template.

<dx:ASPxGridView ID="ABC" runat="server" AutoGenerateColumns="False" DataSourceID="ABCDataSource" EnableTheming="True" Width="100%" ClientInstanceName="gridABC"> <Columns> <dx:GridViewCommandColumn Caption="Actions" ShowInCustomizationForm="True" VisibleIndex="0"> <EditButton Visible="True"> </EditButton> </dx:GridViewCommandColumn> <dx:GridViewDataComboBoxColumn Caption="List of Type" FieldName="ListType" ShowInCustomizationForm="True" VisibleIndex="4" Width="100px"> <PropertiesComboBox DataSourceID="ListTypeDataSource" TextField="ListTypeABC" ValueField="ListTypeABCId" Width="100px"> <ValidationSettings> <RequiredField ErrorText="ListType required" IsRequired="True" /> </ValidationSettings> </PropertiesComboBox> <EditFormSettings ColumnSpan="1" VisibleIndex="1" /> </dx:GridViewDataComboBoxColumn> <dx:GridViewDataTextColumn Caption="Name" FieldName="Name" ShowInCustomizationForm="True" VisibleIndex="2" Width="100px"> <PropertiesTextEdit MaxLength="30" Width="100px"> <ValidationSettings> <RequiredField ErrorText="Item Name required" IsRequired="False" /> </ValidationSettings> </PropertiesTextEdit> <EditFormSettings ColumnSpan="1" VisibleIndex="2" /> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn Caption="Contact" FieldName="Contact" ShowInCustomizationForm="True" VisibleIndex="3" Width="100px"> <PropertiesTextEdit MaxLength="30" Width="100px"> <ValidationSettings> <RequiredField ErrorText="Contact" IsRequired="False" /> </ValidationSettings> </PropertiesTextEdit> <EditFormSettings ColumnSpan="1" VisibleIndex="3" /> </dx:GridViewDataTextColumn> <Templates> <EditForm> <table> <tr> <td>`<dx:ASPxGridViewTemplateReplacement ReplacementType="EditFormEditors" ID="ASPxGridViewTemplateReplacement1" runat="server"> </dx:ASPxGridViewTemplateReplacement>

`

Answer1:

The simplest approach is to set the DropDownList's AutoPostBack property to true and handle it's SelectedIndexChanged event. Then you can Enable/Disable the validator there.

Another approach is to use a CustomValidator. This validator is not dependent on a single control. You must write the validation rules on your own. For example the 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 to always implement a OnServerValidate because you should not rely on javascript only(can be disabled). This is easy:

void ServerValidation(object source, ServerValidateEventArgs args) { args.IsValid = DropDownList1.SelectedIndex == 1 || TextBox1.Text.Length > 0; }

Recommend

  • CoSign API: Signing a PDF stream using SAPI?
  • Mysql dynamic trigger creation in stored procedure
  • How to set a default value when a field is null in jibx binding?
  • How can I sort in a round robin fashion in Lucene?
  • Xstream XML to Java fails with Classcast exception
  • onblur function call in handlebar template
  • Create NestedContent Items In SurfaceController
  • Express JS Node JS multer intergration issue
  • Invoking lambda expressions in Expression trees
  • Iterating through fields with specific attribute
  • How to find type of the field with specific name in Roslyn
  • Cross domain issue with .ajax (JQuery) to a domain I own — simple PHP page
  • How to get font size of form field on iText7?
  • Architecture for communication between two microservices
  • Using JSP C Tag within AngularJS code
  • How to compare two rows or get the fields which value is not match with the rows that compared?
  • Retrieve Greek letters from MSSQL database with PHP
  • How to throw an exception, if an object structure is not matching/fitting another one
  • Describe relationship in maximo 7.5
  • How to create a TFS alert for changes to the items' Stack Rank field
  • Get column names and values in for loop PHP
  • Cannot access sitecore item field via API
  • ONLY Visible Div form elements will get submitted
  • How to know if a JFrame is on screen in a multi screen environment
  • Accessing another variable in the same class with a click event
  • Why can't UI components be accessed from a backgroundworker?
  • How to getText() from the input field of an angularjs Application
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Arrays break string types in Julia
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • Turn off referential integrity in Derby? is it possible?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Add sale price programmatically to product variations
  • Binding checkboxes to object values in AngularJs
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?