Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

Cutom validator for comparing the selected values of 2 drop down lists

$
0
0

Hello Laughing,

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.


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>