I have a project which has a checkbox list that contains code and name of various countries, for example: value =91, country=india etc. taking input from user, I want to add new value + country name to the existing checkbox list. I have the following code as of now. the list is made as follows:
<asp:CheckBoxList ID="cblCountry" runat="server">
<asp:ListItem Value="91" Text="India"></asp:ListItem>
<asp:ListItem Value="92" Text="Nepal"></asp:ListItem>
<asp:ListItem Value="93" Text="Sri Lanka"></asp:ListItem>
<asp:ListItem Value="94" Text="China"></asp:ListItem>
<asp:ListItem Value="95" Text="Pakistan"></asp:ListItem>
<asp:ListItem Value="96" Text="Bangladesh"></asp:ListItem>
</asp:CheckBoxList>
I have set up validation to not allow duplicate value or text as follows in c# code:
for (int i = cblCountry.Items.Count - 1; i >= 0; i--)
{
if(liCountry.Value==cblCountry.Items[i].Value || liCountry.Text==cblCountry.Items[i].Text.Trim())
{
lblMessage.Text ="This code or Name is already taken for another Country.";
flag = true;
break;
}
else
{
flag = false;
}
}
I have to implement the logic that allows new entry only if both value and name of country have never been repeated. the entry should be denied if either value or code or both are repeated. This is to be implemented with the help of CheckBoxList.Items.Contains() method, but since this method compares the whole item, I don't know how to compare either duplicate value or duplicate name separately. Please help me out with this. Thanks.