开发者

asp CompareValidator on 2 dropdowns

开发者 https://www.devze.com 2023-04-10 11:37 出处:网络
Scenario: I have the following controls: <telerik:RadComboBox ID=\"dd1\" runat=\"server\" ValidationGroup=\"g1\" InitialValue=\"-1\" />

Scenario:

I have the following controls:

<telerik:RadComboBox ID="dd1" runat="server" ValidationGroup="g1" InitialValue="-1" />
<telerik:RadComboBox ID="dd2" runat="server" ValidationGroup="g1" InitialValue="-1" />
<asp:CompareValidator ID="cv" runat="server" ValidationGroup="g1" InitialValue="-1"
                    ControlToValidate="dd1" ControlToCompare="dd2" Operator="NotEqual" Text="error"
                    Type="String" />

I don't want the 2 dropDowns to have the same val开发者_C百科ues, excluding "-1" which is the default value of any dropdown.

Can I achieve this using the compareValidator? or should i use javascript?

thanks in advance


For comparision, compare validator is correct. But for initial value (-1), you need to add required field validators for both dropdowns. So user must select value.

<telerik:RadComboBox ID="dd1" runat="server" ValidationGroup="g1" InitialValue="-1" /> 
<asp:RequiredFieldValidator ID="reqv1" runat="server" ErrorMessage="Please select value" ControlToValidate="dd1" ValidationGroup="g1" InitialValue="-1"></asp:RequiredFieldValidator>
<telerik:RadComboBox ID="dd2" runat="server" ValidationGroup="g1" InitialValue="-1" /> 
<asp:RequiredFieldValidator ID="reqv2" runat="server" ErrorMessage="Please select value" ControlToValidate="dd2" ValidationGroup="g1" InitialValue="-1"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="cv" runat="server" ValidationGroup="g1" InitialValue="-1" 
                    ControlToValidate="dd1" ControlToCompare="dd2" Operator="NotEqual" Text="error" 
                    Type="String" /> 


A single CompareValidator by itself will not do what you need, but you can just add another CompareValidator to your setup there. A typical ASP.NET DropDownList doesn't have a default value if empty. It's based on the first item in the list. I'm not sure if the telerik controls you are using have a default value of -1, but if they do, then you could add one or two CompareValidators for each dropdown and set the ValueToCompare attribute, and check for NotEqual:

<asp:CompareValidator ID="cv3" runat="server" ValidationGroup="g1" 
ControlToValidate="lst1" ValueToCompare="-1" Operator="NotEqual" 
Text="Empty value is not allowed" Type="String" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号