Hello ,
I am trying to use ASP.NET validation controls to prevent users from selecting the same item in the 2 drop down lists "dlBookSelectionA" and "dlBookSelectionB" on my ASP.NET C# web form.
I have two drop down list controls as shown below.
<asp:DropDownList ID="dlBookSelectionA" runat="server"><asp:ListItem Value="0" Selected="True">Your first selection</asp:ListItem><asp:ListItem Value="BookA">BookA</asp:ListItem><asp:ListItem Value="BookB">BookB</asp:ListItem><asp:ListItem Value="BookC">BookC</asp:ListItem></asp:DropDownList><asp:DropDownList ID="dlBookSelectionB" runat="server"><asp:ListItem Value="0" Selected="True">Your second selection</asp:ListItem><asp:ListItem Value="BookA">BookA</asp:ListItem><asp:ListItem Value="BookB">BookB</asp:ListItem><asp:ListItem Value="BookC">BookC</asp:ListItem></asp:DropDownList>
After googling & reading some IT books, I have tried the Custom Validator control as shown below, but as I have expected, it didn't work.
<asp:CustomValidator ID="cvCompareDLValues" runat="server" ControlToValidate="dlBookSelectionA"
OnServerValidate="CompareDLValues" ErrorMessage="Please select a different book for your second selection." />
Code behind: protected void CompareDLValues(object source, System.Web.UI.WebControls.ServerValidateEventArgs args) { if (dlBookSelectionA.SelectedIndex == dlBookSelectionB.SelectedIndex) { args.IsValid = false; } }
Please give me the correct markup/code behind code.
Thank you.