Hi,
I have a checkboxlist in my site and I DON'T want users to select more than 3 from the list. When the users select more than 3, I want to show an error message.
Can anyone help me to do it, please?
Thanks
Hi,
I have a checkboxlist in my site and I DON'T want users to select more than 3 from the list. When the users select more than 3, I want to show an error message.
Can anyone help me to do it, please?
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
ozkan atmaca14 reputationPosted Jan 28, 2009, 7:10 AM EST
this client code (js)
protected void Page_Load(object sender, EventArgs e)
{
string control = "chck=0; for (i=0; i < " + CheckBoxList1.Items.Count + "; ++i) if (document.getElementById('"+ CheckBoxList1.ClientID +"$' + i).checked) chck++; if (chck > 3){ alert('warning!!!'); return false;} else return true;";
Button1.Attributes.Add("onclick", control);
}
this server side code
protected void Button1_Click1(object sender, EventArgs e)
{
byte check = 0;
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
check++;
}
if (check > 3)
return;
}
ozkan atmaca14 reputationPosted Jan 28, 2009, 8:40 AM EST
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
byte check = 0;
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected)
check++;
}
if (check > 3)
{
CustomValidator1.ErrorMessage = "Warning !!! ";
args.IsValid = false;
return;
}
args.IsValid = true;
}
r pRank #4,639 · 333 reputationPosted Jan 29, 2009, 5:43 AM EST
I am using wizard steps. Because the next step in the wizard has another checkboxlist which will display values based the first selections and so on.
Thanks