Search code examples
c#asp.netvb.netcomparevalidator

Compare validator is getting active on any click


 <asp:ComboBox ID="ddlto" runat="server" IncrementalFilteringMode="StartsWith"
                DataSourceID="XmlDatasource1" TextField="name" ValueField="name"
                Width="100%" DataTextField="name" DataValueField="name" MaxLength="0" style="display: inline;" 
                                                AutoCompleteMode="Suggest" 
                                                onselectedindexchanged="ddlto_SelectedIndexChanged" />

                                        </td></br>

                          <asp:CompareValidator ID="cm1" runat="server" Operator="NotEqual" Type="string" ControlToValidate="ddlfrom" ControlToCompare="ddlto" ErrorMessage="The 'Departure City' and 'Destination City' cannot be same. Please re-type." ForeColor="red" 
BackColor="yellow" Display="Dynamic" SetFocusOnError="True"> </asp:CompareValidator

It is working properly for that page but when i am clicknig on any other button of that page to redirect to other page again the same messaage of compare validator is printing and i cannot redirect to another page.Means i have 3 pages bus,train and flight when i am removing this compare validator code then i am able to redirext the pages..but when i place these code..in my asp page then, when i click on flight button to redirect to the flight page,it is showing the same message as i have typd in compare validator code.it is not redirecting.PLZZ HELP


Solution

  • You need to add ValidationGroup property to validator and button which should do validation

    <asp:CompareValidator ID="ddlto" ... ValidationGroup="MyValidation"... />
    
    <asp:Button ID="MyBtn" ... ValidationGroup="MyValidation"... />
    

    Just make sure, that validator and button have same group.