Search This Blog

Wednesday, July 14, 2010

How can set the maximum limit of selections in a CheckBoxList

How can set the maximum limit of selections in a CheckBoxList?
hi
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;
}

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;
}

No comments:

Post a Comment