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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
r pPosted Jan 29, 2009, 10:43 AM
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
ozkan atmacaPosted Jan 28, 2009, 1:40 PM
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;
}
ozkan atmacaPosted Jan 28, 2009, 12:10 PM
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;
}